Spring Boot displays a "whitelabel" error page by default as part of auto-configuration. In this tutorial you will learn how to create a custom error page using different view resolvers.

The easiest way to customize the error page is to create a custom view that will resolve for a view named "error". Ultimately this depends on the view resolvers in place when the error view is being resolved. This means in practice, any bean that implements Spring’s View interface and has a bean ID of "error" resolved by Spring’s BeanNameViewResolver.

This mean in practice that the following page will be searched:

  • A Thymeleaf template named “error.html” if Thymeleaf is configured
  • A FreeMarker template named “error.ftl” if FreeMarker is configured
  • A Velocity template named “error.vm” if Velocity is configured
  • A JSP template named “error.jsp” if using JSP views

Adding a standard error page with Thymeleaf

This custom error template should be named "error.html" and placed in the templates directory for the Thymeleaf template resolver to find. For a typical Maven build, that means putting it in src/main/resources/templates

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
   <head>
      <style>div {
background-color:lightblue;
}</style>
   </head>
   <h1>Error!</h1>
   <div class="errorPage">
      <br />
      <p>
         There seems to be a problem with the page you requested
(
         <span th:text="${path}" />
         ).
      </p>
      <p th:text="${'Details: ' + message}" />
      <p th:text="${'Status: ' + status}" />
   </div>
</html>

Here is our project tree with the custom error page:

├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── demo
│   │   │               └── DemoApplication.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── static
│   │       └── templates
│   │           └── error.html

Now if you request a page not found you will stumble upon your error page:

camel spring boot

As you can see, some attributes are available automatically in the view. By default, Spring Boot makes the following error attributes available to the error view:

timestamp —The time that the error occurred

status —The HTTP status code

error —The error reason

exception —The class name of the exception

message —The exception message (if the error was caused by an exception)

errors —Any errors from a BindingResult exception (if the error was caused by an exception)

trace —The exception stack trace (if the error was caused by an exception)

path —The URL path requested when the error occurred

In case you want to enable the stacktrace to be included as expression attribute to our Thymeleaf view, you need to configure in src/main/resources/application.properties

server.error.include-stacktrace=always

Using a custom JSP error page

If you are using JSP as view resolver, then you can place an error page named "error.jsp" in the folder src/main/webapp/WEB-INF/views .

Here is a sample page which is able to output some of the exported error status variables:

<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<body>
    <h1>Oops!!!</h1>
    <table>
          <tr>
            <td>Error</td>
            <td>${error}</td>
        </tr>
        <tr>
            <td>Status</td>
            <td>${status}</td>
        </tr>
        <tr>
            <td>Message</td>
            <td>${message}</td>
        </tr>
        <tr>
            <td>Exception</td>
            <td>${exception}</td>
        </tr>
        <tr>
            <td>Trace</td>
            <td>
                <pre>${trace}</pre>
            </td>
        </tr>
    </table>
</body>
</html>

Besides it, we have to disable in src/main/resources/application.properties the default whitelabel page and enable the stacktrace to be included as expression attribute to our view.

spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always
FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials