[100% solved*] whitelabel error page how to fix (genuine method)?
Spring Boot uses a default Whitelabel error page in case server error. this can be not terribly useful and that we might want to grant additional relevant info to the client in a very production surroundings. this text focuses on the Spring Boot whitelabel error page. we are going to learn the way to disable this default behavior and the way we are able to use our own custom error page to align with our UI.
Let’s study the Whitelabel error page in Spring Boot and the way to customise or disable them. White label error pages area unit default behavior from Spring Boot. like all alternative feature, we are able to customise this feature to nice extent.
What do you know Whitelabel error pages in Spring Boot?
Depending on API consumer request or browser request, spring boot provides a slip-up JSON response or a full hypertext markup language error page. as an example, let’s produce a straightforward /hello end point that throws Associate in Nursing exception forever.
@RequestMapping("/hello")
String hello() {
throw new IntrovertException("Don't bother me please..!");
}
Code language: Java (java)
Even though the messages area unit useful, this page might not match well along with your alternative page styles. therefore if you would like to override this page along with your own style, you're in luck.
server.error.include-message=always
server.error.include-exception=true
server.error.include-stacktrace=always
server.error.include-binding-errors=always
Code language: Properties (properties)
Overriding Whitelabel Error Pages?
Spring boot provides a /error mapping at a worldwide servlet instrumentality level. This mapping handles requests and sends back JSON or hypertext markup language hold a response with error codes/messages. however the read that we tend to saw higher than appearance default. If you notice the primary line of the error page, it says “This application has no express mapping for /error, therefore you're seeing this as a disengagement.”
Here, the spring boot is making an attempt to hint to you that you just have to be compelled to give your own example to handle these error requests. therefore let’s see a way to do this.
As we know, The handler mapped for /error expects a read to indicate the hypertext markup language response. If it doesn’t realize a read matching “error” it'll use the placeholder we've got seen higher than. therefore we tend to 1st have to be compelled to add a example called error.html. however the example alone won't work. you furthermore may have to be compelled to add one in all the spring boot supported template engines. . In our case, we tend to area unit adding thymeleaf.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Code language: HTML, XML (xml)
Code language: hypertext markup language, XML (xml),Next, you wish to feature the error.html template into your src/main/resources/templates directory.With the higher than in situ, the subsequent MVC attributes are going to be accessible for you to access within the templates.
message – come back worth of exception.getMessage()
exception – A string that contains the canonical exception name.
trace – the whole stacktrace of the exception that caused this error.
errors – a listing of validation failures that occured throughout the request.
Along with these, there's conjointly a standing attribute that provides the protocol status code for the error response. therewith in situ, we are able to rewrite our example file to indicate of these attributes.
<!doctype html>
<html lang="en">
<head>
<title th:text="${message}"></title>
</head>
<body>
<table border="1">
<tr><td>Error Message</td><td th:text="${message}"></td></tr>
<tr><td>Status Code</td><td th:text="${status}"></td></tr>
<tr><td>Exception</td><td th:text="${exception}"></td></tr>
<tr><td>Stacktrace</td><td><pre th:text="${trace}"></pre></td></tr>
<tr><td>Binding Errors</td><td th:text="${errors}"></td></tr>
</table>
</body>
</html>
Code language: HTML, XML (xml)
Error Message Status Code Exception Stacktrace Binding Errors
Code language: hypertext markup language, XML (xml),This simple example can yield the subsequent error page once we access /hello.
With a touch little bit of CSS, we are able to get this page to seem higher and additional appealing
Disabling Whitelabel error page altogether /Tomcat whitelabel
Spring boot conjointly provides some way to disable the Whitelabel error page altogether using server.error.whitelabel.enabled setting. once set to false, the server can show a slip-up page specific to the servlet container(tomcat). as an example, the below error page from tom are going to be visible if the Whitelabel is disabled and no error example is on the market.
You can swap tom with groin and you may still see a slip-up page like this offered by the groin runtime. And undertow presently doesn’t give a read. however it will send response codes.
Important things to notice
Always use a custom error.html page for the subsequent reasons.
✅Default whitelabel page lets hackers apprehend you're mistreatment spring boot. this implies they solely have to be compelled to strive the exploits for spring boot.
✅Never show exceptions in your production servers. Exceptions area unit nice information for hackers.
✅A custom error page with correct CSS can mix in to your alternative pages. you'll be able to even give links and search boxes which will send users back to your website.
✅You can hide specific error attributes supported the configuration we tend to saw earlier. Also, of these configurations also are applicable for the JSON response still. If your request contains Accept: application/json header, then the response are going to be within the variety of JSON. Even here, you'll be able to access of these attributes. as an example, take a glance at the below request.
Here you'll be able to see the trace, exception, and message attributes being accessible as JSON.
Spring boot custom error page?
<!DOCTYPE html> <html lang="en"> <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; - public </p> </body> </html>
<!DOCTYPE html> <html> <head> <title>Error occurred</title> </head> <body> <h1>Error occurred</h1> <p> An error has occurred. Please contact the administrator; - template generic </p> </body> </html>
A generic error page using a template can
<!DOCTYPE html> <html lang="en"> <html 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>.
<!DOCTYPE html> <html lang="en"> <html 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>
Spring custom error page example?
gradle.build ... src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───controller │ │ MyController.java │ └───resources │ │ application.properties │ └───templates │ └───error │ 404.html └───test └───java
plugins { id 'org.springframework.boot' version '2.6.7' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.zetcode' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' } test { useJUnitPlatform()
#server.error.whitelabel.enabled=false #spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
package org.websparrow.controller;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ErrorrHandlerController implements ErrorController {
@GetMapping("/error")
public String customError() {
return "The link you followed may be broken, or the page may have been removed.";
}
@Override
public String getErrorPath() {
return "/error";
}
}
Conclusion:-
To total it up, we tend to learned concerning white label error pages and the way to customise them. we tend to got wind a way to override the default Whitelabel with our own error.html. you'll be able to consider of these complete source code is available over on Github,Thank you.
EmoticonEmoticon