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
這里。

慕虎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); }}
添加回答
舉報
0/150
提交
取消