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

今天在做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将仅扫描类包下面的类,因此我们要确保主类在其他类上的根包中。

 


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;

@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.boot package

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.boot package

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

具体如下图所示:




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值