In a Spring Boot application, the default error page that is displayed when an exception occurs is called the “Whitelabel Error Page”. This page displays information about the exception, such as the exception message, stack trace, and a timestamp. While this page can be useful for debugging purposes, it can also be a security concern as it may reveal sensitive information about the application. Additionally, it may not be desirable from a user experience standpoint as it does not provide a professional look and feel.
In this blog post, we will show you how to remove the Whitelabel Error Page in Spring Boot and configure a custom error page.
Step 1: Create a custom error page
Create a new HTML file in the src/main/resources/templates
directory of your Spring Boot project. This file will be the custom error page that will be displayed when an exception occurs. For example, you can create a file called error.html
with the following content:
Copy code<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>An error has occurred</h1>
<p>We apologize for the inconvenience. Please try again later or contact us if the problem persists.</p>
</body>
</html>
Step 2: Create a error controller
Create a new controller class in your project and annotate it with @Controller
. The class should handle all error related request. For example:
Copy code@Controller
public class ErrorController {
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if (statusCode == HttpStatus.NOT_FOUND.value()) {
return "error-404";
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "error-500";
}
}
return "error";
}
}
Step 3: Configure Spring Boot to use the custom error page
In the application.properties
file, set the server.error.whitelabel.enabled
property to false
. This disables the Whitelabel Error Page and tells Spring Boot to use the custom error page that we created in step 1.
Copy codeserver.error.whitelabel.enabled=false
Step 4: Test the custom error page
Start the Spring Boot application and navigate to a non-existent page. The custom error page that we created in step 1 should be displayed.
Additional step : Adding error attributes
You can also add custom error attributes to the model and access them in the error page. To do this, you can create a class that implements the ErrorAttributes
interface and overrides its methods. For example:
@Component
public class CustomErrorAttributes implements ErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> errorAttributes = new HashMap<>();
errorAttributes.put("timestamp", new Date());
errorAttributes.put("status", 500);
errorAttributes.put("error", "Internal Server Error");
errorAttributes.put("message", "An internal server error occurred. Please contact support.");
errorAttributes.put("path", webRequest.getAttribute("javax.servlet.error.request_uri", RequestAttributes.SCOPE_REQUEST));
return errorAttributes;
}
}
You can then add the class as a bean in your @Configuration
class :
@Bean
public ErrorAttributes errorAttributes() {
return new CustomErrorAttributes();
}
You can now access these attributes in the error page and display them to the user. For example:
<p>Error Code: ${status}</p>
<p>Error Message: ${message}</p>
Additional step: Handling specific exception
You can also handle specific exceptions and redirect to specific error pages. To do this, you can create a @ControllerAdvice
class and use the @ExceptionHandler
annotation to handle specific exceptions. For example:
@ControllerAdvice
public class ExceptionControllerAdvice {
@ExceptionHandler(value = {IllegalArgumentException.class, IllegalStateException.class})
public ModelAndView handleIllegalArgumentException(RuntimeException ex) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("error", ex.getMessage());
modelAndView.setViewName("error-400");
return modelAndView;
}
}
In this example, if an IllegalArgumentException
or an IllegalStateException
is thrown, the handleIllegalArgumentException
method will be called and the user will be redirected to the error-400
page.
By following the steps above, you can customize the error page to be more user-friendly and secure. Additionally, you can also handle specific exceptions and redirect to specific error pages. This can provide a better user experience for your application and help you quickly identify and fix problems.
Conclusion
In this blog post, we have shown you how to remove the Whitelabel Error Page in Spring Boot and configure a custom error page. By following the steps above, you can create a more professional and user-friendly error page for your Spring Boot application. Additionally, you can also handle different error codes and redirect to different error pages based on the error codes.