为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Boot删除Whitelabel错误页面

Spring Boot删除Whitelabel错误页面

弑天下 2019-08-09 17:39:47
Spring Boot删除Whitelabel错误页面我正在尝试删除白标错误页面,所以我所做的是为“/ error”创建了一个控制器映射,@RestControllerpublic 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文件中,仍然会收到相同的错误
查看完整描述

3 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

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

@RestControllerpublic 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 这里


查看完整回答
反对 回复 2019-08-09
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

如果你想要一个更“JSONish”的响应页面,你可以试试这样的东西:

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.web.ErrorAttributes;import org.springframework.boot.autoconfigure.web.ErrorController;import org.springframework.util.Assert;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import java.util.Map;@RestController@RequestMapping("/error")public class SimpleErrorController implements ErrorController {

  private final ErrorAttributes errorAttributes;

  @Autowired
  public SimpleErrorController(ErrorAttributes errorAttributes) {
    Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
    this.errorAttributes = errorAttributes;
  }

  @Override
  public String getErrorPath() {
    return "/error";
  }

  @RequestMapping
  public Map<String, Object> error(HttpServletRequest aRequest){
     Map<String, Object> body = getErrorAttributes(aRequest,getTraceParameter(aRequest));
     String trace = (String) body.get("trace");
     if(trace != null){
       String[] lines = trace.split("\n\t");
       body.put("trace", lines);
     }
     return body;
  }

  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 aRequest, boolean includeStackTrace) {
    RequestAttributes requestAttributes = new ServletRequestAttributes(aRequest);
    return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
  }}


查看完整回答
反对 回复 2019-08-09
  • 3 回答
  • 0 关注
  • 868 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信