小编典典

应用程序没有针对/ error的显式映射

spring

应用程序可以运行,但是出现错误:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Jun 30 17:24:02 CST 2015 There was an unexpected error (type=Not Found, status=404). No message available

我该如何解决?


阅读 2691

收藏
2020-04-11

共1个答案

小编典典

当我们创建一个Spring Boot应用程序时,我们用注解对其进行@SpringBootApplication注解。该批注“包装”了许多其他必需的批注,以使应用程序正常工作。一种这样的注释是@ComponentScan注释。该注释告诉Spring寻找Spring组件并配置要运行的应用程序。

你的应用程序类必须位于程序包层次结构的顶部,以便Spring可以扫描子程序包并找到其他必需的组件。

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);
    }
}

下面的代码片段适用于控制器程序包在com.test.spring.boot程序包下的情况

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!";
    }
}

以下代码段 不起作用,因为控制器软件包不在com.test.spring.boot软件包中

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!";
     }
 }

从Spring Boot文档中:

许多sspring引导开发者总是有其主类注解为@Configuration@EnableAutoConfiguration@ComponentScan。由于这些注释经常一起使用(特别是如果你遵循上述最佳实践),因此Spring Boot提供了一种方便的@SpringBootApplication替代方法。

@SpringBootApplication注解相当于使用 @Configuration@EnableAutoConfiguration@ComponentScan与他们的默认属性

2020-04-11