Spring Boot删除Whitelabel错误页面

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文件中,仍然会收到相同的错误


弑天下
浏览 871回答 3
3回答

繁花如伊

您需要将代码更改为以下内容:@RestControllerpublic&nbsp;class&nbsp;IndexController&nbsp;implements&nbsp;ErrorController{ &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;String&nbsp;PATH&nbsp;=&nbsp;"/error"; &nbsp;&nbsp;&nbsp;&nbsp;@RequestMapping(value&nbsp;=&nbsp;PATH) &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;error()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;"Error&nbsp;handling"; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;getErrorPath()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;PATH; &nbsp;&nbsp;&nbsp;&nbsp;}}您的代码不起作用,因为BasicErrorController当您没有指定实现时,Spring Boot会自动将其注册为Spring Bean&nbsp;ErrorController。要看到这个事实,只需导航到ErrorMvcAutoConfiguration.basicErrorController&nbsp;这里。

慕虎7371278

如果你想要一个更“JSONish”的响应页面,你可以试试这样的东西:import&nbsp;org.springframework.beans.factory.annotation.Autowired;import&nbsp;org.springframework.boot.autoconfigure.web.ErrorAttributes;import&nbsp;org.springframework.boot.autoconfigure.web.ErrorController;import&nbsp;org.springframework.util.Assert;import&nbsp;org.springframework.web.bind.annotation.RequestMapping;import&nbsp;org.springframework.web.bind.annotation.RestController;import&nbsp;org.springframework.web.context.request.RequestAttributes;import&nbsp;org.springframework.web.context.request.ServletRequestAttributes;import&nbsp;javax.servlet.http.HttpServletRequest;import&nbsp;java.util.Map;@RestController@RequestMapping("/error")public&nbsp;class&nbsp;SimpleErrorController&nbsp;implements&nbsp;ErrorController&nbsp;{ &nbsp;&nbsp;private&nbsp;final&nbsp;ErrorAttributes&nbsp;errorAttributes; &nbsp;&nbsp;@Autowired &nbsp;&nbsp;public&nbsp;SimpleErrorController(ErrorAttributes&nbsp;errorAttributes)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Assert.notNull(errorAttributes,&nbsp;"ErrorAttributes&nbsp;must&nbsp;not&nbsp;be&nbsp;null"); &nbsp;&nbsp;&nbsp;&nbsp;this.errorAttributes&nbsp;=&nbsp;errorAttributes; &nbsp;&nbsp;} &nbsp;&nbsp;@Override &nbsp;&nbsp;public&nbsp;String&nbsp;getErrorPath()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;"/error"; &nbsp;&nbsp;} &nbsp;&nbsp;@RequestMapping &nbsp;&nbsp;public&nbsp;Map<String,&nbsp;Object>&nbsp;error(HttpServletRequest&nbsp;aRequest){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map<String,&nbsp;Object>&nbsp;body&nbsp;=&nbsp;getErrorAttributes(aRequest,getTraceParameter(aRequest)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;trace&nbsp;=&nbsp;(String)&nbsp;body.get("trace"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(trace&nbsp;!=&nbsp;null){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;lines&nbsp;=&nbsp;trace.split("\n\t"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body.put("trace",&nbsp;lines); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;body; &nbsp;&nbsp;} &nbsp;&nbsp;private&nbsp;boolean&nbsp;getTraceParameter(HttpServletRequest&nbsp;request)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;parameter&nbsp;=&nbsp;request.getParameter("trace"); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(parameter&nbsp;==&nbsp;null)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;!"false".equals(parameter.toLowerCase()); &nbsp;&nbsp;} &nbsp;&nbsp;private&nbsp;Map<String,&nbsp;Object>&nbsp;getErrorAttributes(HttpServletRequest&nbsp;aRequest,&nbsp;boolean&nbsp;includeStackTrace)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;RequestAttributes&nbsp;requestAttributes&nbsp;=&nbsp;new&nbsp;ServletRequestAttributes(aRequest); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;errorAttributes.getErrorAttributes(requestAttributes,&nbsp;includeStackTrace); &nbsp;&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP