浏览:623 | 回复:11
浏览器请求spring-boot模块项目报:There was an unexpected error (type=Not Found, status=404).
heartsmile
  • 注册时间:2015-03-20
  • 最后登入:2017-12-31

推荐


模块包结构如下:

控制器类代码如下:

@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模板

[html] view plain copy
  1. <code class="language-html"><parent>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-parent</artifactId>  
  4.     <version>1.4.2.RELEASE</version>  
  5. </parent>  
  6. <dependency>      
  7.     <groupId>org.springframework.boot</groupId>    
  8.     <artifactId>spring-boot-starter-web</artifactId>  
  9. </dependency></code>  
    <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()


业精于勤,荒于嬉;行成于思,毁于随