Javatpoint Logo
Javatpoint Logo

Implementing Generic Exception Handling for all Resources

As we discussed earlier that we should define a standard exception structure which is followed across all the RESTful services. In this section, we will discuss the implementation of generic exception handling for all resources.

Let's see how to customize the exception message.

Step 1: Create a new package with name com.javatpoint.server.main.exception.

Step 2: Create a class with the name ExceptionResponse in the above package.

Step 3: At the basic level, there are three crucial things for the exception structure: timestamp, message, and detail. Define these three fields.

Step 4: Generate Constructors using Fields.

Step 5: Generate Getters, Setters are not needed.

Once the structure is defined, we can define Java implementation for it. Whenever an exception occurs, we would return a response in the specific format. The structure is the most important part and must have to be language independent.

One of the important class in Spring Framework is ResponseEntityExceptionHandler class. It is an abstract and base class for the exceptions that provide centralized exception handling across all the different exception handler methods. We will extend this class to handle and provide customized exception handling functionality. This exception handling functionality is applied to all controller as HellowWorldController, UserResource (Controller).

Step 6: Create a new class with name CustomizedResponseEntityExceptionHandler in the package com.javatpoint.server.main.exception and extends ResponseEntityExceptionHandler class.

Step 7: Add @ControllerAdvice and @RestController annotations.

Step 8: Expand Maven Dependencies in the package explorer -> Expand spring-webmvc-5.1.9.RELEASE.jar -> Expand org.springframework.web.servlet.mvc.method.annotation -> Open ResponseEntityExceptionHandler.class.

Implementing Generic Exception Handling for all Resources

Step 9: Copy the ResponseEntityMethod<Object> method from ResponseEntityExceptionHandler.class and paste in CustomizedResponseEntityExceptionHandler.java file.

Step 10: Override the ResponseEntityMethod method. Rename the method name as handleAllExceptions().

Step 11: Create the exception response structure.

Step 12: Create a ResponseEntity object and pass the exception response and Http status as arguments.

CustomizedResponseEntityExceptionHandler.java

Step 13: Open rest client Postman and send a Get request. We get the Status: 500 Internal Server Error and exception structure which we have defined.

Implementing Generic Exception Handling for all Resources

If we want to customize the status Internal Server Error to Not Found, we are required to change a few things in CustomizedResponseEntityExceptionHandler.java file.

CustomizedResponseEntityExceptionHandler.java

Again move to Postman and send a Get request. We get the Status: 404 Not Found with the defined exception structure.

Implementing Generic Exception Handling for all Resources







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA