Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot and custom 404 error page

In my Spring Boot application, I'm trying to configure custom error pages, for example for 404, I have added a following Bean to my application configuration:

@Bean public EmbeddedServletContainerCustomizer containerCustomizer() {     return new EmbeddedServletContainerCustomizer() {         @Override         public void customize(ConfigurableEmbeddedServletContainer container) {             container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));         }     }; } 

Also, I have created a following simple Thymeleaf template:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">     <head>         <title>404 Not Found</title>         <meta charset="utf-8" />     </head>     <body>         <h3>404 Not Found</h3>         <h1 th:text="${errorCode}">404</h1>         <p th:utext="${errorMessage}">Error java.lang.NullPointerException</p>         <a href="/" th:href="@{/}">Back to Home Page</a>     </body> </html> 

and added it into /resources/templates/ folder. Right now on the 404 error I can see only white screen.

What am I doing wrong and how to correctly setup my 404 page? Also, is it possible to use templates and not only static pages for custom error pages?

like image 452
brunoid Avatar asked May 23 '16 18:05

brunoid


People also ask

How do I create a 404 page in spring boot?

We first need to create a custom HTML error page. If we save this file in resources/templates directory, it'll automatically be picked up by the default Spring Boot's BasicErrorController. We can be more specific by naming the file with the HTTP status code we want it used e.g. saving the file as 404.

How do I redirect a custom error page in spring boot?

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.


2 Answers

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> 
like image 61
brunoid Avatar answered Sep 28 '22 00:09

brunoid


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.

like image 42
GoutamS Avatar answered Sep 28 '22 00:09

GoutamS