How do you create a custom error page in Spring Boot?

Table of Contents

Introduction

In Spring Boot, error handling is an important aspect of web application development. By default, Spring Boot provides basic error pages for common HTTP errors (like 404 or 500). However, you may want to customize these error pages to improve user experience and provide more meaningful feedback. Spring Boot makes it easy to create custom error pages for various HTTP status codes and exceptions.

This guide explains how to create custom error pages in Spring Boot, including handling specific HTTP errors, customizing error messages, and using @ControllerAdvice for centralized exception handling.

1. Customizing Error Pages for HTTP Status Codes

1.1. Using error.html for General Error Handling

Spring Boot provides a default error page for common HTTP errors like 404 (Not Found) and 500 (Internal Server Error). To customize the error pages for these HTTP status codes, you can create a error.html page in the src/main/resources/templates directory (for Thymeleaf views).

Example: Creating a Custom error.html

In this example:

  • status will display the HTTP status code (e.g., 404, 500).
  • message will provide a generic error message.
  • The page will display a simple message and a link to return to the homepage.

Spring Boot automatically uses this page for general error handling, such as when a 404 error occurs.

1.2. Customizing Error Pages for Specific HTTP Status Codes

To create custom pages for specific HTTP status codes like 404 (Not Found) or 500 (Internal Server Error), you can create separate error page templates. Spring Boot will automatically map these error pages to the corresponding HTTP status codes.

Example: Customizing the 404 Error Page

  1. Create a template called error404.html for the 404 error:

  2. Create a template called error500.html for the 500 error:

By creating error404.html and error500.html in the src/main/resources/templates directory, Spring Boot will use them to handle 404 and 500 errors, respectively.

1.3. Creating Custom Error Pages Using application.properties

You can also configure Spring Boot to use specific error pages for different HTTP statuses by specifying the paths in application.properties:

This configuration will allow you to define a custom error handler URL (/error) and map it to a specific page.

1.4. Handling Specific Error Pages Using ErrorController

You can implement the ErrorController interface to define a custom controller that handles error requests.

Example: Custom ErrorController Implementation

In this example:

  • The CustomErrorController handles errors by checking the ERROR_STATUS_CODE attribute in the request.
  • Depending on the HTTP status code, it returns the corresponding error page (e.g., error404.html for a 404 error).
  • The getErrorPath() method returns the URL path (/error) that Spring Boot uses for error handling.

2. Handling Exceptions with @ControllerAdvice

In addition to customizing HTTP error pages, you may want to handle specific exceptions globally. Spring Boot provides @ControllerAdvice, a powerful way to manage exceptions across the entire application.

2.1. Creating Global Exception Handler with @ControllerAdvice

You can use @ControllerAdvice to create global exception handling for various types of exceptions.

Example: Global Exception Handler

In this example:

  • The @ExceptionHandler annotation catches specific exceptions (ResourceNotFoundException and Exception).
  • When a ResourceNotFoundException occurs, it directs the user to the custom 404 page (error404.html).
  • If a general exception occurs, it displays the custom 500 error page (error500.html).

2.2. Adding Custom Exception Class

To use ResourceNotFoundException in the example above, you can create a custom exception class:

3. Using Error Attributes for Customization

You can customize the error response by adding custom error attributes. Spring Boot provides the ErrorAttributes interface, which allows you to define custom error details that are returned in the response body.

Example: Custom ErrorAttributes Implementation

In this example:

  • The CustomErrorAttributes class defines custom error attributes, which can be included in the response body for error handling.
  • You can customize the error message, status, and other details as needed.

Conclusion

Customizing error pages in Spring Boot is essential for enhancing the user experience and providing clear, helpful feedback when errors occur. You can create custom error pages for different HTTP statuses, implement global exception handling with @ControllerAdvice, and even customize the error response using ErrorAttributes. Whether you're handling form validation errors, exceptions, or specific HTTP errors like 404 and 500, Spring Boot offers powerful tools to create a seamless error handling experience.

Similar Questions