处理Whitelabel Error Page

来源:互联网 发布:产业结构优化理论 编辑:程序博客网 时间:2024/05/18 09:19

今天在学习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方法的那个类)引入了一段定义错误页面的代码:

@Beanpublic 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.csdn.net/github_32521685/article/details/50198467



原创粉丝点击