AXIAL

Custom HTML Error Page using Thymeleaf

In this article, we’ll learn how to customize default Whitelabel Error Page in Spring Boot using Thymeleaf HTML templates

Whitelabel Error Page

Whitelabel error page is default error page generated by Spring Boot when no custom error page is provided.


If you don’t like this default behavior and looking for custom HTML pages to be displayed for different type of HTTP errors then Thymeleaf error templates are simplest approach to follow.

Thymeleaf Error Templates

You create Thymeleaf HTML error template under resources/templates directory:-

  1. Generic error template required with name error.html
  2. Specific error templates are optional under errors directory with name .html for e.g. 404.html for Not found error, 500.html for Internal server error, etc.

Generic Error Template

If specific error template not found then fallback to generic template for all types of HTTP error codes.

resources/templates/error.html
</span>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Something went wrong!title>
head>
<body>
  <h1>Generic Error - <span th:text="${status}">Statusspan>h1>
  <p>Sorry for the inconvenience. Please contact the administrator.p>
  <br/>
  <ul>
    <li>Timestamp: <span th:text="${timestamp}">Timestampspan>li>
    <li>Path: <span th:text="${path}">Pathspan>li>
    <li>Error: <span th:text="${error}">Errorspan>li>
  ul>
body>
html>

Specific error template 500.html doesn’t exist, display error.html


Specific Error Template

If specific error template found for e.g. 404.html, display it for 404 error!
We imported bootstrap css in the 404 error page to look more elegant.

resources/templates/errors/404.html
</span>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  <title>404 - resource not foundtitle>
head>
<body>
<div class="container">
  <h1><span class="label label-default">Resource Not Found Error - 404span>h1>

  <p>Requested resource was not found. Please contact the administrator.p>
  <table class="table table-bordered table-striped">
    <tr>
      <td>Timestamptd>
      <td th:text="${timestamp}">Timestamptd>
    tr>
    <tr>
      <td>Pathtd>
      <td th:text="${path}">Pathtd>
    tr>
    <tr>
      <td>Statustd>
      <td th:text="${status}">Statustd>
    tr>
    <tr>
      <td>Errortd>
      <td th:text="${error}">Errortd>
    tr>
    <tr>
      <td>Messagetd>
      <td th:text="${message}">Messagetd>
    tr>
  table>
  div>
body>
html>

Specific error template 400.html exist, display it!


Thymeleaf Dependency

We need to add Thymeleaf dependency in the project for these error templates to work.

build.gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
pom.xml

    org.springframework.boot
    spring-boot-starter-thymeleaf

Thats it! Once you add Thymeleaf dependency, it does all the magic.

Related Articles

Back to top button