Boot删除Whitelabel错误页面

Boot删除Whitelabel错误页面

本文介绍了Spring Boot删除Whitelabel错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除白标签错误页面,所以我所做的是为"/error"创建了一个控制器映射,

I'm trying to remove white label error page, so what I've done was created a controller mapping for "/error",

@RestController
public class IndexController {

    @RequestMapping(value = "/error")
    public String error() {
        return "Error handling";
    }

}

但是现在我遇到了这个错误.

But now I"m getting this error.

Exception in thread "AWT-EventQueue-0" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource   [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Invocation  of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'basicErrorController' bean method
public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>>  org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletR equest)
to {[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'indexController' bean method

不知道我做错了什么.请指教.

Don't know whether I'm doing anything wrong. Please advice.

已添加 error.whitelabel.enabled=false到application.properties文件,仍然出现相同的错误

Already added error.whitelabel.enabled=false to application.properties file, still getting the same error

推荐答案

您需要将代码更改为以下内容:

You need to change your code to the following:

@RestController
public class IndexController implements ErrorController{

    private static final String PATH = "/error";

    @RequestMapping(value = PATH)
    public String error() {
        return "Error handling";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}

您的代码不起作用,因为当您未指定ErrorController的实现时,Spring Boot会自动将BasicErrorController注册为Spring Bean.

Your code did not work, because Spring Boot automatically registers the BasicErrorController as a Spring Bean when you have not specified an implementation of ErrorController.

要查看该事实,只需导航至ErrorMvcAutoConfiguration.basicErrorController .

To see that fact just navigate to ErrorMvcAutoConfiguration.basicErrorController here.

这篇关于Spring Boot删除Whitelabel错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:43