Spring Boot custom error pages, whitelabel error page processing mode

Source: Internet
Author: User
Tags html5 boilerplate

I'm already a big fan of the spring framework Framework. For enterprise software developers, It provides a common solution to common problems, including those that you are unaware of in future Development. however, the Java EE project it builds has become bloated and needs to be replaced by a new Solution.

My biggest complaint is that the initial use of the Spring Framework build project is very slow and complex, such as building an MVC application with Jpa. To change this situation, Spring boot came into being.

Spring boot replaces the traditional way of building projects with the spring framework in a new microservices approach, which I have planned to use in subsequent project Development. It has helped us do 90% of the work and the remaining 10% of the work needs to be done by Ourselves. For me, the custom error page is one of Them. For example, 404 error, If Not processed, will appear "this application have no explicit mapping for/error, so is seeing this as a fallback." The error message,:

Spring boot uses embedded tomcat by default, with no page to handle common errors such as 404. therefore, in order to give the user the best experience, 404 Common errors need our custom page to Handle.

We need to use the Org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer class to implement this function, in spring Boot class (the Main method is in the same class), add the following code:

@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); });}

This is the way to simplify the implementation by using a lambda expression in Java 8, creating three ErrorPage instances in the code to handle three common HTTP error status codes and adding them to the Container. The ErrorPage class is a class that encapsulates the error message and can be used in jetty and Tomcat Environments.

This is an equivalent implementation of using a Java 7 inner class:

@Beanpublic embeddedservletcontainercustomizer  Containercustomizer () {return new Embeddedservletcontainercustomizer () { @Override public Span class= "hljs-keyword" >void customize (configurableembeddedservletcontainer 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); } };}

The error page needs to be placed in the static content directory of the spring Boot Web app, and its default location is: src/main/resources/static, as shown in:

Click on the link to download a compressed file of three files in the diagram, which are made using HTML5 Boilerplate.

now, you can see a simple version of the error page, you can according to your own needs to beautify it.

Compiled from: http://www.sporcic.org/2014/05/custom-error-pages-with-spring-boot/.

http://blog.csdn.net/github_32521685/article/details/50198467

Spring Boot custom error pages, whitelabel error page processing mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.