- Mastering Spring Boot 2.0
- Dinesh Rajput
- 179字
- 2021-06-25 21:29:17
Customizing application error pages
Every application has a chance of encountering an error, even if it is an extremely robust application. So, designing custom error pages is important for any enterprise application. Spring Boot applications provide a default error page. You can see one in the following screenshot:
![](https://epubservercos.yuewen.com/A0BBDD/19470392708881706/epubprivate/OEBPS/Images/Chapter_180.jpg?sign=1739642918-aRgYyAi4elQMyhHoVSIaguusGPSo2Ip8-0-c0d28febb22a7ba2e2dd69df6e73f6f9)
But if you want to use a custom error page for a given status code, you can add a file to the /error folder. You can create a custom error page by using static HTML, FreeMarker, Velocity, Thymeleaf, JSP, and so on. The name of the file should be the exact status code or a series mask.
Let's see the following image to map 404 to a static HTML file; your folder structure would be as follows:
![](https://epubservercos.yuewen.com/A0BBDD/19470392708881706/epubprivate/OEBPS/Images/Chapter_146.jpg?sign=1739642918-4sKHEvsxiguXVLPPStEmDDGdMb5dssE3-0-bda3d88385031c205934faf344b1d4c9)
As you can see, I have added a custom 404 error page (static error page 404.html) under the /resource/public/error directory; see the following output from the error page:
![](https://epubservercos.yuewen.com/A0BBDD/19470392708881706/epubprivate/OEBPS/Images/Chapter_159.jpg?sign=1739642918-xR5YlXnhopjrSEcDOmuTtSq2unDXix2V-0-2eaf88471475a4140a9b86ba1b8f9ba5)
As you can see, Spring Boot now displays a custom error page instead of the default WhiteLabel error page as part of auto-configuration.