This article would demo how to use custom static html error page to replace springboot’s default whitelabel error page.
2. Environments
SpringBoot 1.x or SpringBoot 2.x
3. Define your own ErrorHandler
According to spring documents, if there is no ErrorHandler found, then you would get a white label error page like this:
Let’s define our own ErrorHandler:
This ErrorHandler serves the path ‘/error’ and return an error page named ‘/myError’, you can define a @Controller to serve the /myError link like this:
Then you can start your app and visit the url like this:
4. Serve error pages using static html contents
4.1 Add Custom Html files to your project
Create some static html error pages to your project like this:
For example, the myError404.html content is:
The myError500.html is:
The myErrorUnknown.html is:
4.2 Write your ErrorHandler to point to static html files
4.3 Test
When we visit a url that is not exist, we get this:
When we visit a url that throws an exception internally, we get this:
It works!
5. Summary
You can see that replace the default error page of springboot or spring applications is very esay.