Spring Boot 移除 Whitelabel 错误页面

新手上路,请多包涵

我正在尝试删除白标错误页面,所以我所做的是为“/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 文件中,仍然出现相同的错误

原文由 Yasitha Waduge 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 515
2 个回答

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

 @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。

要查看这一事实,只需导航到 ErrorMvcAutoConfiguration.basicErrorController 此处

原文由 geoand 发布,翻译遵循 CC BY-SA 3.0 许可协议

此处 的手册说您必须将 server.error.whitelabel.enabled 设置为 false 以禁用标准错误页面。也许这就是你想要的?

顺便说一句,我在添加 /error 映射后遇到了同样的错误。

原文由 Innot Kauker 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题