Spring Boot and custom 404 error page Spring Boot and custom 404 error page java java

Spring Boot and custom 404 error page


In Spring Boot 1.4.x you can add a custom error page:

If you want to display a custom HTML error page for a given status code, you add a file to an /error folder. Error pages can either be static HTML (i.e. added under any of the static resource folders) or built using templates. The name of the file should be the exact status code or a series mask.

For example, to map 404 to a static HTML file, your folder structure would look like this:

src/ +- main/     +- java/     |   + <source code>     +- resources/         +- public/             +- error/             |   +- 404.html             +- <other public assets>


You're using Thymeleaf, And Thymeleaf can handle error without a controller.

For a generic error page this Thymeleaf page need to be named as error.html
and should be placed under src/main/resources > templates > error.html

Thymleaf error.html

For specific error pages, you need to create files named as the http error code in a folder named error, like: src/main/resources/templates/error/404.html.


new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")

That /404.html represents the URL Path to Redirect, not the template name. Since, you insist to use a template, you should create a controller that handles the /404.html and renders your 404.html resides in src/main/resources/templates:

@Controllerpublic class NotFoundController {    @RequestMapping("/404.html")    public String render404(Model model) {        // Add model attributes        return "404";    }}

You could also replace these just view render-er controllers with a View Controller:

@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter {    @Override    public void addViewControllers(ViewControllerRegistry registry) {        registry.addViewController("/404.html").setViewName("404");    }}

Also, is it possible to use templates and not only static pages for custom error pages ?

Yes, it's possible. But Not Found pages are usually static and using a template instead of Plain Old HTMLs wouldn't make that much of a sense.