Spring Boot删除白标签错误页面

我试图删除白标签错误页面,所以我所做的是创build了一个控制器映射为“/错误”,

@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 

不知道我是否做错了什么。 请指教。

编辑:

已经向application.properties文件添加了error.whitelabel.enabled=false ,仍然收到相同的错误

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

 @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

如果你想要一个更“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); } } 

您可以通过指定完全删除它:

 import org.springframework.context.annotation.Configuration; import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration; ... @Configuration @EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class}) public static MainApp { ... } 

但是,请注意,这样做可能会导致servlet容器的whitelabel页面显示出来:)


编辑:另一种方法来做到这一点是通过application.yaml。 只要把价值:

 spring: autoconfigure: exclude: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration 

文档

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

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

春季启动文档 '是'错误(固定):

要closures它,你可以设置error.whitelabel.enabled = false

应该

要closures它,你可以设置server.error.whitelabel.enabled = false

使用Spring Boot> 1.4.x,你可以这样做:

 @SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class}) public class MyApi { public static void main(String[] args) { SpringApplication.run(App.class, args); } } 

但是如果发生exception,servlet容器将显示自己的错误页面。

在使用Mustache模板的Spring Boot 1.4.1中,在templates文件夹下放置error.html就足够了:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Error</title> </head> <body> <h1>Error {{ status }}</h1> <p>{{ error }}</p> <p>{{ message }}</p> <p>{{ path }}</p> </body> </html> 

其他variables可以通过为/error创build一个拦截器来传递

server.error.whitelabel.enabled = FALSE

将以上行添加到资源文件夹application.properties中

更多的错误问题解决请参考http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-customize-the-whitelabel-error-page