欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

浏览器请求spring-boot模块项目报:There was an unexpected error (type=Not Found, status=404).

程序员文章站 2022-03-03 17:05:42
...

模块包结构如下:

浏览器请求spring-boot模块项目报:There was an unexpected error (type=Not Found, status=404).

控制器类代码如下:

@RestController
@RequestMapping("/dispatchCenter")
public class DispatchCenterController {
  
    private static final Logger LOGGER = LoggerFactory.getLogger(DispatchCenterController.class);

    @RequestMapping(value = "/doMain",method = RequestMethod.GET)
    public String dispatchCenter(@RequestParam(value = "seqNo") String seqNo){

        System.out.print(seqNo);

        return "hello spring-boot";
    }
}
在浏览器中输入http://localhost:7071/dispatchCenter/doMain?seqNo=%22hello%22报如下错误:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Mar 25 17:38:52 CST 2018
There was an unexpected error (type=Not Found, status=404).
No message available

1、首先查看在pom中配置或者是有没有正确加载,以及你是否使用了thymeleaf模板

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>
<dependency>    
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

2、在浏览器中输入的URL是否正确,spring-boot项目是不需要跟spring项目一样还要带上项目名称

http://localhost:7073/rabbitMq/helloQueue2

3、Application启动类文件的包必须是项目下的父路径,其他类的包路径必须是其子路径,虽然放在其它包下也能正常启动,但是就是在浏览器中访问不到,因为handleMapper扫描不到,可以在启动类上加上注解

@ComponentScan(basePackages = {"com.spring.*"})

4、最后我的原因是第三点所描述的问题,于是将DispatchCenterApplication.java移动到com.dispatchCenter包下,启动后成功解决问题。

5、如果不是以上问题,就去查看spring-boot项目的启动日志,看看o.s.w.s.m.m.a.RequestMappingHandlerMapping中有没有你所配置的handelMapping,如:

@RestController
@RequestMapping("/rabbitMq")
public class HelloRabbitMQController {
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**实现消息的发送*/
    @RequestMapping(value = "/helloQueue",method = RequestMethod.GET)
    public void send(){
        String message = "this is queue test!";
        this.amqpTemplate.convertAndSend("hello-queue",message);
    }
    @RequestMapping(value = "/helloQueue2",method = RequestMethod.GET)
    public void send2(){

    }
}

日志中应该会如下信息说明就成功扫描到并注册了handelMapping,如果没有就仔细检查路径的配置,还有就是@RequestMapping(value="*") 这个value是不是写成name了

2018-04-05 17:06:39.059 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/rabbitMq/helloQueue],methods=[GET]}" onto public void com.dataService.controller.HelloRabbitMQController.send()
2018-04-05 17:06:39.059 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/rabbitMq/helloQueue2],methods=[GET]}" onto public void com.dataService.controller.HelloRabbitMQController.send2()