Spring Boot - Custom Error Page in Thymeleaf

[Last Updated: Feb 26, 2018]

This example shows how to replace the default whitelabel error page with a custom Thymeleaf error page in Spring Boot.

Create a controller

public class MyController {

  public void handleRequest() {
      throw new RuntimeException("test exception");

A custom Thymeleaf error page

Spring Boot by default provides /error mapping where all exception/errors are forwarded. In case of Thymeleaf (or other template engines), we can map errors to a global custom template file by name 'error' under src/main/resources/templates/ directory.


<!DOCTYPE html>
<html xmlns=""
table td{
border:solid 1px #888;

<h1>My Thymeleaf Error Page</h1>
        <td th:text="${timestamp}"/>
        <td th:text="${path}"/>
        <td th:text="${error}"/>
        <td th:text="${status}"/>
        <td th:text="${message}"/>
        <td th:text="${exception}"/>
            <pre th:text="${trace}"/>

Spring Boot file

We are enabling the stacktrace to be included as expression attribute to our Thymeleaf view.



Main class

public class SpringBootMain extends SpringBootServletInitializer {

  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
      return builder.sources(SpringBootMain.class);

  public static void main(String[] args) {;
mvn spring-boot-run


To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

We can also run the main class from our IDE.

Accessing 'http:/localhost:8080/'

Accessing unmapped URL, say 'http:/localhost:8080/other'

Like our last example, deploying to Tomcat server or running the Boot executable war also works without an problems.

Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.5.10.RELEASE
    Corresponding Spring Version 4.3.14.RELEASE
  • spring-boot-starter-thymeleaf : Starter for building MVC web applications using Thymeleaf views.
    Uses org.thymeleaf:thymeleaf-spring4
  • JDK 1.8
  • Maven 3.3.9

Boot Custom Jsp Error Page Example Select All Download
  • custom-thymeleaf-error-page
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • templates
            • error.html

    See Also