一尘不染

Spring Boot删除Whitelabel错误页面

spring

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

@RestController
public class IndexController {

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

}

但是现在我得到了这个错误。

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

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

编辑:

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


阅读 460

收藏
2020-04-11

共1个答案

一尘不染

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

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

你的代码无法正常工作,因为BasicErrorController当你未指定的实现时,Spring Boot会自动将其注册为Spring Bean ErrorController

要查看该事实,请导航至ErrorMvcAutoConfiguration.basicErrorController 此处。

2020-04-11