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

Den*_*eve 84 upload spring file-upload spring-mvc

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

应用程序可以运行,但我收到错误:

Whitelabel错误页面此应用程序没有/ error的显式映射,因此您将此视为回退.Tue Jun 30 17:24:02 CST 2015出现意外错误(type = Not Found,status = 404).没有可用的消息

我该如何解决?

vig*_*ash 113

确保您的主类位于其他类之上的根包中.

当您运行Spring Boot Application(即使用@SpringBootApplication注释的类)时,Spring将仅扫描主类包下面的类.

com
   +- APP
         +- Application.java  <--- your main class should be here, above your controller classes
         |
         +- model
         |   +- user.java
         +- controller
             +- UserController.java
Run Code Online (Sandbox Code Playgroud)

  • 我花了将近2小时的时间来搞清楚这一点! (13认同)
  • 高于或高于同一水平? (3认同)
  • 也尝试过。仍然错误。至少主页(即http:// localhost:8080)应该向我显示Tomcat主页,不是吗?但这也没有显示 (3认同)

mus*_*ibs 47

当我们创建一个Spring启动应用程序时,我们使用注释对其进行@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

下面的代码片段工作,因为控制器包在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!";
    }
}
Run Code Online (Sandbox Code Playgroud)

下面的代码片段 不起作用,因为控制器包不在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!";
     }
 }
Run Code Online (Sandbox Code Playgroud)

从Spring Boot文档:

许多春季引导开发者总是有其主类注解为@Configuration,@EnableAutoConfiguration@ComponentScan.由于这些注释经常一起使用(特别是如果您遵循上面的最佳实践),Spring Boot提供了一个方便的@SpringBootApplication替代方案.

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

  • 非常好的解释.谢谢 (3认同)

owa*_*ism 37

您可以通过ErrorController在应用程序中添加一个来解决此问题.您可以让错误控制器返回您需要的视图.

我的应用程序中的错误控制器如下所示:

import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Basic Controller which is called for unhandled errors
 */
@Controller
public class AppErrorController implements ErrorController{

    /**
     * Error Attributes in the Application
     */
    private ErrorAttributes errorAttributes;

    private final static String ERROR_PATH = "/error";

    /**
     * Controller for the Error Controller
     * @param errorAttributes
     */
    public AppErrorController(ErrorAttributes errorAttributes) {
        this.errorAttributes = errorAttributes;
    }

    /**
     * Supports the HTML Error View
     * @param request
     * @return
     */
    @RequestMapping(value = ERROR_PATH, produces = "text/html")
    public ModelAndView errorHtml(HttpServletRequest request) {
        return new ModelAndView("/errors/error", getErrorAttributes(request, false));
    }

    /**
     * Supports other formats like JSON, XML
     * @param request
     * @return
     */
    @RequestMapping(value = ERROR_PATH)
    @ResponseBody
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
        HttpStatus status = getStatus(request);
        return new ResponseEntity<Map<String, Object>>(body, status);
    }

    /**
     * Returns the path of the error page.
     *
     * @return the error path
     */
    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }


    private boolean getTraceParameter(HttpServletRequest request) {
        String parameter = request.getParameter("trace");
        if (parameter == null) {
            return false;
        }
        return !"false".equals(parameter.toLowerCase());
    }

    private Map<String, Object> getErrorAttributes(HttpServletRequest request,
                                                   boolean includeStackTrace) {
        RequestAttributes requestAttributes = new ServletRequestAttributes(request);
        return this.errorAttributes.getErrorAttributes(requestAttributes,
                includeStackTrace);
    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request
                .getAttribute("javax.servlet.error.status_code");
        if (statusCode != null) {
            try {
                return HttpStatus.valueOf(statusCode);
            }
            catch (Exception ex) {
            }
        }
        return HttpStatus.INTERNAL_SERVER_ERROR;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的类基于Springs BasicErrorController类.

您可以ErrorController@Configuration文件中实例化上述内容:

 @Autowired
 private ErrorAttributes errorAttributes;

 @Bean
 public AppErrorController appErrorController(){return new AppErrorController(errorAttributes);}
Run Code Online (Sandbox Code Playgroud)

您可以ErrorAttributes通过实现ErrorAttributes来选择覆盖默认值.但在大多数情况下,DefaultErrorAttributes应该足够了.


Min*_*wzy 10

在我的情况下,它因为包的位置,意味着控制器的包必须在主类包之上

如果我的主类包是package co.companyname.spring.tutorial;任何控制器包应该package co.companyname.spring.tutorial.WHAT_EVER_HERE;

package co.companyname.spring.tutorial; // package for main class
@SpringBootApplication
public class FirstProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(FirstProjectApplication.class, args);
    }
}


package co.companyname.spring.tutorial.controllers; // package for controllers 

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController 
public class HelloController { 

@RequestMapping("/hello")  
public String hello() {   
 return "Hello, world"; 
 }}
Run Code Online (Sandbox Code Playgroud)

完成编码后按下启动仪表板

在此输入图像描述

最后一件事是确保你的控制器是映射或不只是控制台,你应该看到一些smillar

Mapped "{[/hello]}" onto public java.lang.String co.companyname.spring.tutorial.controllers.HelloController.hello()
Run Code Online (Sandbox Code Playgroud)

快乐的编码


小智 9

默认情况下,spring boot 将扫描当前包以获取 bean 定义。因此,如果您当前的包定义了主类并且控制器包不相同,或者控制器包不是主应用程序包的子包,它将不会扫描控制器。为了解决这个问题,可以在主包中包含 bean 定义的包列表

@SpringBootApplication(scanBasePackages = {"com.module.restapi1.controller"})
Run Code Online (Sandbox Code Playgroud)

或创建一个包层次结构,其中子包从主包派生

package com.module.restapi;
package com.module.restapi.controller
Run Code Online (Sandbox Code Playgroud)


myk*_*key 8

在我的例子中,控制器类用注释@Controller.改变它来@RestController解决问题.基本上@RestController@Controller + @ResponseBody 因此,无论使用@RestController,或@Controller@ResponseBody每个方法注释.

这里有一些有用的注释:https://www.genuitec.com/spring-frameworkrestcontroller-vs-controller/

  • 它可以工作,但根据互联网上所有示例,基本配置应该与 @Controller 一起使用。任何人都知道为什么只有 RestController 工作的原因吗? (2认同)

Sem*_*aca 7

我正在开发 Spring Boot 应用程序几个星期.. 我得到了与下面相同的错误;

白标错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。Thu Jan 18 14:12:11 AST 2018 出现意外错误(type=Not Found,status=404)。没有可用的消息

当我收到此错误消息时,我意识到我的控制器或其余控制器类未在我的项目中定义。我的意思是我们所有的控制器包都与包含 @SpringBootApplication 注释的主类不同。我的意思是您需要将控制器包的名称添加到 @ComponentScan 注释到您的主类,其中包含 @SpringBootApplication 注释。如果您编写下面的代码,您的问题将得到解决......最重要的是您必须像我在下面所做的那样将所有控制器的包添加到@ComponentScan 注释中

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({ "com.controller.package1, com.controller.package2, com.controller.package3, com.controller.packageN", "controller", "service" } // If our Controller class or Service class is not in the same packages we have //to add packages's name like this...directory(package) with main class
public class MainApp {
    public static void main(String[] args) {
        SpringApplication.run(MainApp.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这些代码可以帮助某人...

如果您找到解决此错误的另一种方法,或者您对我有一些建议,请写信给评论...谢谢...


小智 7

在主类中,在配置“@SpringBootApplication”之后,添加“@ComponentScan”而不带任何参数,对我有用!

主要类别:

@SpringBootApplication
@ComponentScan
public class CommentStoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(CommentStoreApplication.class, args);

    }
}
Run Code Online (Sandbox Code Playgroud)

休息控制器类:

@RestController
public class CommentStoreApp {

    @RequestMapping("/") 
    public String hello() {
        return "Hello World!";
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:在启动应用程序之前,不要错过运行 mvn clean 和 mvn install 命令


小智 7

如果未定义显式错误页面,则会发生这种情况。要定义错误页面,请创建带有视图的/ error映射。例如,下面的代码映射到在发生错误的情况下返回的字符串值。

package com.rumango.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class IndexController implements ErrorController{
    private final static String PATH = "/error";
    @Override
    @RequestMapping(PATH)
    @ResponseBody
    public String getErrorPath() {
        // TODO Auto-generated method stub
        return "No Mapping Found";
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 6

尝试添加依赖项.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 这实际上有什么作用? (3认同)

小智 5

我添加了此依赖关系,它解决了我的问题。

<dependency>
    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Rup*_*uka 5

您可能会收到错误,即

“此应用程序没有 /error 的显式映射,因此您将其视为后备。”

这是因为它没有扫描您必须在 main() 类中指定的控制器和服务类,如下所示,

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
**@ComponentScan({"com.example.demo", "controller", "service"})**
public class SpringBootMvcExample1Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMvcExample1Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:这里,我指定了要扫描的各种类,如演示、控制器和服务,只有这样它才能正常工作。