今天在学习Spring Boot实战的时候遇到了Whitelabel Error Page错误,具体提示如下:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Nov 07 16:44:35 CST 2017
There was an unexpected error (type=Not Found, status=404).
No message available
看提示就知道是错误页面存在问题,那么问题是怎么引起的呢,查阅了一下资料得知: Spring Boot默认使用嵌入式Tomcat,默认没有页面来处理404等常见错误。因此,为了给用户最佳的使用体验,404等常见错误需要我们自定义页面来处理。所以我们要做的是自己来定义错误页面。
所以我在Spring Boot的启动类(就是含有main方法的那个类)引入了一段定义错误页面的代码:
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
container.addErrorPages(error401Page, error404Page, error500Page);
});
}至此,实例程序终于可以正常运行。
参考文资料:http://blog.youkuaiyun.com/github_32521685/article/details/50198467
在学习Spring Boot实战中遇到Whitelabel Error Page错误。该错误是因为默认没有404等错误页面的映射。为提供良好用户体验,需要自定义错误页面。解决方案是在启动类中添加代码定义错误页面。

被折叠的 条评论
为什么被折叠?



