Customizing the exception message

Let's look at how to customize the preceding exception and return the proper response status with a customized message.

Let's create a bean to define the structure of our custom exception message:

    public class ExceptionResponse {
private Date timestamp = new Date();
private String message;
private String details;

public ExceptionResponse(String message, String details) {
super();
this.message = message;
this.details = details;
}

public Date getTimestamp() {
return timestamp;
}

public String getMessage() {
return message;
}

public String getDetails() {
return details;
}
}

We have created a simple exception response bean with an auto-populated timestamp with a few additional properties namely messages and details.

When ;TodoNotFoundException is thrown, we would want to return a response using the ExceptionResponse bean. The following code shows how we can create a global exception handling for TodoNotFoundException.class:

    @ControllerAdvice
@RestController
public class RestResponseEntityExceptionHandler
extends ResponseEntityExceptionHandler
{
@ExceptionHandler(TodoNotFoundException.class)
public final ResponseEntity<ExceptionResponse>
todoNotFound(TodoNotFoundException ex) {
ExceptionResponse exceptionResponse =
new ExceptionResponse( ex.getMessage(),
"Any details you would want to add");
return new ResponseEntity<ExceptionResponse>
(exceptionResponse, new HttpHeaders(),
HttpStatus.NOT_FOUND);
}
}

Some important things to note are as follows:

  • RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler: We are extending ;ResponseEntityExceptionHandler, which is the base class provided by Spring MVC for centralised exception handling ControllerAdvice classes.
  • @ExceptionHandler(TodoNotFoundException.class): This defines that the method to follow will handle the specific exception TodoNotFoundException.class. Any other exceptions for which custom exception handling is not defined will follow the default exception handling provided by Spring Boot.
  • ExceptionResponse exceptionResponse = new ExceptionResponse(ex.getMessage(), "Any details you would want to add"): This creates a custom exception response.
  • new ResponseEntity<ExceptionResponse>(exceptionResponse,new HttpHeaders(), HttpStatus.NOT_FOUND): This is the definition to return a 404 Resource Not Found response with the custom exception defined earlier.

When we execute the service with a GET request to a nonexistent ;todo (http://localhost:8080/users/Jack/todos/222), we get the following response:

    {
"timestamp": 1484030343311,
"message": "Todo Not Found",
"details": "Any details you would want to add"
}

 

If you want to create a generic exception message for all exceptions, we can add a method to RestResponseEntityExceptionHandler with the ;@ExceptionHandler(Exception.class) ;annotation.

The following code snippet shows how we can do this:

    @ExceptionHandler(Exception.class)
public final ResponseEntity<ExceptionResponse> todoNotFound(
Exception ex) {
//Customize and return the response
}

 

Any exception for which a custom exception handler is not defined ;will be handled by the preceding method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset