spring boot报错:this application has no explicit mapping for /error...
2017-08-29 09:45
666 查看
今天在做SpringBoot的小例子,在请求controller层出现了以下问题:
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、选择构建工具如下图所示:
![](https://img-blog.csdn.net/20170829100652029?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3lkX3NodWloYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3、点击Generate Project下载项目压缩包。
导入到eclipse中的项目结构如下图:
![](https://img-blog.csdn.net/20170829100721039?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3lkX3NodWloYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
之所以出现上述错误,原因在于:
【Application启动类放的位置不对】要将Application放在最外层,也就是要包含所有子包。
比如你的groupId是com.google,子包就是所谓的com.google.xxx,所以要将Application放在com.google包下。
请参考以下结论:spring-boot会自动加载启动类所在包下及其子包下的所有组件.
当你运行Spring Boot应用程序(即使用@SpringBootApplication注释的类)时,Spring将仅扫描类包下面的类,因此我们要确保主类在其他类上的根包中。
以下内容引用StackoverFlow中的回答:
When we create a Spring boot application we annotate it with
This annotation 'wraps up' many other necessary annotations for the application to work. One such annotation is
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.
This works as the controller package is under
This does NOT Work as the controller package is NOT under
修改完后的项目如下所示:
![](https://img-blog.csdn.net/20170829100757043?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3lkX3NodWloYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
在浏览器输入:http://localhost:8080,可以输出正确结果:
![](https://img-blog.csdn.net/20170829111458045?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3lkX3NodWloYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
如果不想将用户新加的包放在Application所在的包下,就得改变默认扫描包的位置,可参照如下方法:
改变默认扫描包位置(在官网下载的时候指定的包,这里为ZA23.microservice包)的方法:
1.注释掉MicroserviceApplication.java文件里面的main方法,这个是入口类,如果存在多个会引起冲突
2.在自定义的包处添加注解@SpringBootApplication以及main方法
注意:默认的包扫描路径包含ZA23.microservice以及它的子目录,如:ZA23.microservice.Controller
具体如下图所示:
![](https://img-blog.csdn.net/20170829123857204?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3lkX3NodWloYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20170829123909506?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3lkX3NodWloYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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
@SpringBootApplicationannotation.
This annotation 'wraps up' many other necessary annotations for the application to work. One such annotation is
@ComponentScanannotation.
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; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
This works as the controller package is under
com.test.spring.bootpackage
package com.test.spring.boot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @RequestMapping("/") public String home(){ return "Hello World!"; } }
This does NOT Work as the controller package is NOT under
com.test.spring.bootpackage
package com.test.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public 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 |
相关文章推荐
- Spring Boot报错:This application has no explicit mapping for /error......
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- 异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
- Spring Boot 报错Wihtelabel Error Page:This application has no explicit mapping for /error 解决办法
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- SpringBoot启动报错【This application has no explicit mapping for /error】
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- Idea - Spring Boot - MVC 出现This application has no explicit mapping for /error, so you are seeing th
- spring-boot启动报错:This application has no explicit mapping for /error, so you are seeing this as a fal
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- 异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
- SpringBoot报错:Whitelabel Error Page This application has no explicit mapping for /error, so you are s
- Springboot启动后报错【This application has no explicit mapping for /error, so you are seeing this as a fallback····】
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- This application has no explicit mapping for /error, so you are seeing this as a fallback
- This application has no explicit mapping for /error, so you are seeing this as a fallback
- This application has no explicit mapping for /error, so you are seeing this as a fallback
- This application has no explicit mapping for /error, so you are seeing this as a fallback.
- Springboot应用静态资源不能访问,报“This application has no explicit mapping for /error”
- SpringBoot启动报错【This application has no explicit mapping for /error.....】