spring boot报错:this application has no explicit mapping for /error...

来源:互联网 发布:放弃后的心疼网络歌曲 编辑:程序博客网 时间:2024/06/04 19:21

今天在做SpringBoot的小例子,在请求controller层出现了以下问题:

Whitelabel Error Page

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

Tue Aug 29 09:06:14 CST 2017
There was an unexpected error (type=Not Found, status=404).
No message available
======================================================================================================

我的例子是在官网上自动产生的:

1、访问:http://start.spring.io/

2、选择构建工具如下图所示:


3、点击Generate Project下载项目压缩包。

导入到eclipse中的项目结构如下图:


之所以出现上述错误,原因在于:

【Application启动类放的位置不对】要将Application放在最外层,也就是要包含所有子包。

比如你的groupId是com.google,子包就是所谓的com.google.xxx,所以要将Application放在com.google包下。

请参考以下结论:spring-boot会自动加载启动类所在包下及其子包下的所有组件.



当你运行Spring Boot应用程序(即使用@SpringBootApplication注释的类)时,Spring将仅扫描类包下面的类,因此我们要确保主类在其他类上的根包中。

down vote
 

com   +- APP         +- Application.java  <--- your main class should be here, above your controller classes         |         +- model         |   +- user.java         +- controller             +- UserController.java

以下内容引用StackoverFlow中的回答:


When we create a Spring boot application we annotate it with @SpringBootApplication annotation. This annotation 'wraps up' many other necessary annotations for the application to work. One such annotation is @ComponentScan annotation. This annotation tells spring to look for Spring components and configure the application to run.

Your application class needs to be top of your package hierarchy, so that Spring can scan sub-packages and find out the other required components.

package com.test.spring.boot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App {    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }}

This works as the controller package is under com.test.spring.boot package

package com.test.spring.boot.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HomeController {    @RequestMapping("/")    public String home(){        return "Hello World!";    }}

This does NOT Work as the controller package is NOT under com.test.spring.boot package

package com.test.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HomeController {     @RequestMapping("/")     public String home(){         return "Hello World!";     } }

修改完后的项目如下所示:



在浏览器输入:http://localhost:8080,可以输出正确结果:


如果不想将用户新加的包放在Application所在的包下,就得改变默认扫描包的位置,可参照如下方法:

改变默认扫描包位置(在官网下载的时候指定的包,这里为ZA23.microservice包)的方法:

1.注释掉MicroserviceApplication.java文件里面的main方法,这个是入口类,如果存在多个会引起冲突
2.在自定义的包处添加注解@SpringBootApplication以及main方法

注意:默认的包扫描路径包含ZA23.microservice以及它的子目录,如:ZA23.microservice.Controller

具体如下图所示:




down vote
 
阅读全文
0 0
原创粉丝点击