Customizing Whitelabel error in Thymeleaf
Spring Boot by default installs a ‘whitelabel’ error page that you see in a browser client if you encounter a server error. You can customize it as needed based on your view technology, find the below post to override it in thymeleaf.
Step 1: Disable default error page in application.properties
#Disabling Default while lable error server.error.whitelabel.enabled=false
Step 2: Adding a custom error page (error.html)
Overriding the error page with your own depends on the templating technology that you use. Here since I am using Thymeleaf, i have added error.html template.
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>404 - resource not found</title> </head> <body> <h2>404 - Resource not found</h2> <p> The requested resource was not found; template - specific </p> <p th:text="${error}">Error Info</p> <p th:text="${status}">Status</p> </body> </html>
Step 3: Configure pom.xml
Just add web, thymeleaf starter to your pom or gradle file.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.8.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.candidjava</groupId> <artifactId>customwhitelabel_error</artifactId> <version>0.0.1-SNAPSHOT</version> <name>customwhitelabel_error</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Step 4: Launch as Spring boot application (CustomwhitelabelErrorApplication.java)
package com.candidjava.customwhitelabel_error; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CustomwhitelabelErrorApplication { public static void main(String[] args) { SpringApplication.run(CustomwhitelabelErrorApplication.class, args); } }
Download
Download source code from my github account Click here