[英]How to replace Spring Boot's "Whitelabel Error Page" with a blank page?
On any exceptions, by default Spring Boot routes to /error
, which generates an error HTML page:在任何异常情况下,Spring Boot 默认路由到
/error
,它会生成一个错误 HTML 页面:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Oct 31 16:01:01 CET 2018
There was an unexpected error (type=Not Found, status=404)
Question: how can I instead show a blank page by default for a certain endpoint only?问题:我怎样才能在默认情况下只为某个端点显示一个空白页面?
There is a property server.error.whitelabel.enabled=false
, but that disables the error handling entirely, so that the webservers default error page is shown (eg on Tomcat a 404 error page).有一个属性
server.error.whitelabel.enabled=false
,但这会完全禁用错误处理,以便显示网络服务器的默认错误页面(例如,在 Tomcat 上显示 404 错误页面)。
That's not what I want.那不是我想要的。 I'd just like to show a blank plain page.
我只想显示一个空白的普通页面。 Or so to say: an empty response body.
或者可以这么说:一个空的响应体。 But how?
但是如何?
Because, for development and testing the "Whitelabel Error Page" is fine.因为,对于开发和测试,“白标错误页面”很好。 But in production I'd like to hide any exception details entirely.
但在生产中,我想完全隐藏任何异常细节。
I think this tutorial describes the way to go: https://www.baeldung.com/spring-boot-custom-error-page我认为本教程描述了要走的路: https : //www.baeldung.com/spring-boot-custom-error-page
Essentially you would do something like this:基本上你会做这样的事情:
@Controller
public class MyErrorController implements org.springframework.boot.web.servlet.error.ErrorController {
@RequestMapping("/error")
public String handleError() {
//do something like logging
return "error";
}
@Override
public String getErrorPath() {
return "/error";
}
}
And then you'd make sure the default behaviour is turned off.然后你要确保默认行为是关闭的。
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
@SpringBootApplication
public class Application {
public static void main(String... args) {
SpringApplication.run(Application.class, args);
}
}
I just discovered that this "Whitelabel Error Page" is only shown inside a web browser requesting a text/html
content type .我刚刚发现这个“白标错误页面”仅显示在请求
text/html
content type的网络浏览器中。
If a Spring REST webservice is accessed from a native client using just application/json
, the error is translated to JSON automatically.如果仅使用
application/json
从本机客户端访问 Spring REST Web 服务,则错误会自动转换为 JSON。
In case one would really want to show a blank page for web browsers, one could just override the BasicErrorController#errorHtml()
bean method somehow.如果你真的想为 Web 浏览器显示一个空白页面,你可以以某种方式覆盖
BasicErrorController#errorHtml()
bean 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.