Spring Boot 自定义 Whitelabel Error Page

我试图删除springboot的 Whitelabel Error Page 页面。我的做法是添加了一个 /error 映射的Controller

@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

代码不起作用,因为当没有指定ErrorController的实现时,SpringBoot会自动将 BasicErrorController 注册为Spring Bean

修改Controller,作为 ErrorController的实现

@RestController
public class IndexController implements ErrorController{

    private static final String PATH = "/error";
	private static final String PATH = "/error";

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