【springboot】解决springboot项目,扫描不到自定义的controller

如果遇到 http://localhost:8080/test访问出现这个异常This application has no explicit mapping for /error, so you are seeing this as a fallback.

【springboot】解决springboot项目,扫描不到自定义的controller_第1张图片

 可是已经检查了路径没有错误,那很有可能就是springboot没有扫描到自定义的controller

【springboot】解决springboot项目,扫描不到自定义的controller_第2张图片

1.首先检查一下启动类和自定义类的位置是否正确,以确保springboot能够自动加载。

注意:springboot会自动扫描启动类同一个父目录下以及同一个父目录的子目录(侄子目录)的所有组件

【springboot】解决springboot项目,扫描不到自定义的controller_第3张图片

2.检查一下controller类的注释是否正确

在controller类,用的注释是@RestController或者是@Controller + @ResponseBody,而非@Controller。

@RestController:所有方法定义的Response的响应体是String或者json时使用

@Controller + @ResponseBody:想要做页面跳转时使用,

如果需要部分方法返回值是String和json,则在类上添加 @Controller,在需要返回 String 和 json 的方法上添加 @ResponseBody 注解;

3.查看import的包是否正确

@RequestMapping("/test")
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
import org.springframework.web.bind.annotation.RestController;

4.@RequestMapping(“xxx”) 的URL路径书写问题

注意不要有空格,并且必须在前面加上 “/”

5.检查项目启动端口配置是否正确

你可能感兴趣的:(Springboot,spring,boot,java,spring)