DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Using JdbcTemplate With Spring Boot and Thymeleaf | Spring Boot Tutorial
  • Getting Started With Thymeleaf In Spring Boot
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project

Trending

  • Web Crawling for RAG With Crawl4AI
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Infrastructure as Code (IaC) Beyond the Basics
  1. DZone
  2. Coding
  3. Frameworks
  4. Custom Error Page with Thymeleaf

Custom Error Page with Thymeleaf

Errors happen, but when they do, your user deserves more information than a whitelabel error page.

By 
Siddhartha Bhattacharjee user avatar
Siddhartha Bhattacharjee
·
Jun. 14, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
18.1K Views

Join the DZone community and get the full member experience.

Join For Free

Image title


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

By default, a Spring boot application will show the Default Whitelabel Error page, which is good in the beginning but eventually, we might want to show a more suitable Error page with more useful details.

This is an image of default Whitelabel Error Page:

Spring Boot provides default mapping for /error to which all exception or error responses are forwarded. That's why any error (like a 404 error) or Exception thrown from the Controller will map to the /error page and our custom error page will be rendered.

We will first define an HTML file with the name error.html. Here I have used bootstrap CSS to style the page and added some of the information like the time stamp, Date, Error message etc. in the HTML page.

src/main/resources/templates/error.html:


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet" type="text/css" th:href="@{~/css/bootstrap.min.css}" />
<link rel="stylesheet" type="text/css" th:href="@{~/css/style.default.css}" />
</head>
<body marginwidth="0" marginheight="0">
  <div class="container">
    <div class="row">
      <h1>Thymeleaf Error Page</h1>
    </div>
    <div class="row">It apperrs Either something went wrong or the page
      doesn't exist anymore.....</div>
    <div class="row rowDesign">
      <div class="col-md-4 ml-auto">
        <span class="legend">Date</span>
      </div>
      <div class="col-md-8">
        <span th:text="${timestamp}"></span>
      </div>
    </div>
    <div class="row rowDesign">
      <div class="col-md-4 ml-auto">
        <span class="legend">Path</span>
      </div>
      <div class="col-md-8">
        <span th:text="${path}"></span>
      </div>
    </div>
    <div class="row rowDesign">
      <div class="col-md-4 ml-auto">
        <span class="legend">Error</span>
      </div>
      <div class="col-md-8">
        <span th:text="${error}"></span>
      </div>
    </div>
    <div class="row rowDesign">
      <div class="col-md-4 ml-auto">
        <span class="legend">Status</span>
      </div>
      <div class="col-md-8">
        <span th:text="${status}"></span>
      </div>
    </div>
    <div class="row rowDesign">
      <div class="col-md-4 ml-auto">
        <span class="legend">Message</span>
      </div>
      <div class="col-md-8">
        <span th:text="${message}"></span>
      </div>
    </div>
    <div class="row rowDesign">
      <div class="col-md-4 ml-auto">
        <span class="legend">Exception</span>
      </div>
      <div class="col-md-8">
        <span th:text="${exception}"></span>
      </div>
    </div>
    <div class="row rowDesign">
      <div class="col-md-4 ml-auto">
        <span class="legend">Trace</span>
      </div>
      <div class="col-md-8">
        <span th:text="${trace}"></span>
      </div>
    </div>
  </div>
</body>
</html>

Apart from the error message, if we want to show the stack trace, then we need to set the following properties.

src/main/resources/application.properties:

server.error.include-stacktrace=always


Once these changes are done, let's start the Spring Boot App by executing following command.

mvn spring-boot:run


Now for any error pages our custom error page should be displayed. For instance, for a 404 error (for a page that does not exist), it should display something like this:

Image title

Github repo for this article can be found here. 

Spring Framework Thymeleaf Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Using JdbcTemplate With Spring Boot and Thymeleaf | Spring Boot Tutorial
  • Getting Started With Thymeleaf In Spring Boot
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!