此应用程序没有明确的/error 映射 [英] This application has no explicit mapping for /error

查看:22
本文介绍了此应用程序没有明确的/error 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 maven 做教程 https://spring.io/guides/gs/uploading-files/
我使用的所有代码都是复制的.

I used maven to do the tutorial https://spring.io/guides/gs/uploading-files/
All the codes I used was copied.

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

The Application can run, but I get the error:

Whitelabel 错误页面 此应用程序没有明确的/error 映射,因此您将其视为后备.2015 年 6 月 30 日星期二 17:24:02 CST 出现意外错误(类型 = 未找到,状态 = 404).没有可用的消息

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

我该如何解决?

推荐答案

当我们创建一个 Spring Boot 应用程序时,我们用 @SpringBootApplication 注释来注释它.此注释包含"了应用程序运行所需的许多其他注释.其中一种注释是 @ComponentScan 注释.该注解告诉 Spring 查找 Spring 组件并配置要运行的应用程序.

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.

您的应用程序类需要位于包层次结构的顶部,以便 Spring 可以扫描子包并找出其他所需的组件.

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

下面的代码片段有效,因为控制器包在com.test.spring.boot包下

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

下面的代码片段不起作用,因为控制器包不在com.test.spring.boot包下

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

来自 Spring Boot 文档:

From Spring Boot documentation:

许多 Spring Boot 开发人员总是对他们的主类进行注释使用 @Configuration@EnableAutoConfiguration@ComponentScan.由于这些注释经常一起使用(特别是如果您遵循上面的最佳实践),Spring Boot 提供了一个方便的 @SpringBootApplication 替代方案.

Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.

@SpringBootApplication 注解相当于使用@Configuration@EnableAutoConfiguration@ComponentScan 以及它们的默认属性

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes

这篇关于此应用程序没有明确的/error 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆