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.
我的例子是在官网上自动产生的:
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将仅扫描类包下面的类,因此我们要确保主类在其他类上的根包中。
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包)的方法:
具体如下图所示:
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- spring boot报错:this application has no explicit mapping for /error...
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- Spring Boot报错:This application has no explicit mapping for /error......
- Spring-boot 报错This application has no explicit mapping for /error, so you are seeing this as a fallb
- 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 ... a fallback
- Spring Boot报错:This application has no explicit mapping ... a fallback
- SpringBoot启动报错【This application has no explicit mapping for /error.....】
- 《Springboot极简教程》问题解决:Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping for
- springboot访问页面报错:This application has no explicit mapping for /error, so you are seeing this as a fa
- 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
- linux下IPTABLES配置详解
- Linux下C语言连接MySQL
- PAT各种迷之坑点
- Eclipse遇到的问题总结
- Java中堆内存和栈内存详解
- spring boot报错:this application has no explicit mapping for /error...
- oracle数据库,java读取空间数据类型JGeometry
- android项目引用多个library导致引用android-support-v4.jar冲突
- 用adb命令安装apk到手机
- linux中makefile与gcc编译的比较
- 杂记1--对MVC模型理解
- HDU 5713 K个联通块(状压DP)
- 卷积神经网络CNN-目标分类
- Silverlight一直报错 4004 资源无法访问 添加字体也无法解决