Exception Handling In Spring Boot



As we all know exception handling is the most important and crucial thing in SpringBoot Rest APIs, which helps us to perform conditional and unconditional checking for our code and handle any kind of exception in a proper way. Along with its benefits it also complicates the code and makes the code not easily readable to the unknown users.


So we need to have a common place where we can manage and handle all kinds of exceptions and send the respective error code for the API response depending on the exception types.


In this blog we will try to know a simple way which will make our code be in better format related to handling of exceptions provided in SpringBoot.


Description

SpringBoot provides a very powerful annotation called @ControllerAdvice under package org.springframework.web.bind.annotation. This annotation makes our life easy to handle all kinds of exceptions at a central place in our application. We don’t need to catch any exception at each method or class separately instead you can just throw the exception from the method and then it will be caught under the central exception handler class annotated by @ControllerAdvice. Any class annotated with @ControllerAdvice will become a controller-advice class which will be responsible for handling exceptions. Under this class we make use of annotations provided as @ExceptionHandler, @ModelAttribute, @InitBinder.


@ControllerAdvice is a specialisation of the @Component annotation which allows handling exceptions across the whole application in one global handling component. It can be viewed as an interceptor of exceptions thrown by methods annotated with @RequestMapping and similar.


ResponseEntityExceptionHandler is a convenient base class for @ControllerAdvice classes that wish to provide centralised exception handling across all @RequestMapping methods through @ExceptionHandler methods. It provides methods for handling internal Spring MVC exceptions. It returns a ResponseEntity in contrast to DefaultHandlerExceptionResolver which returns a ModelAndView.


Exception handling methods annotated with @ExceptionHandler will catch the exception thrown by the declared class and we can perform various things whenever we come through the related type exceptions. We can catch various exceptions and throw various http status codes depending on the exception which we need to handle. Below example illustrates how we can catch various exceptions and send respective http status codes accordingly.

@ExceptionHandler(value = { DataNotFoundException.class })

public ResponseEntity<ResponseDTO<Object>> dataNotFoundException(InValidDataException ex) {

ResponseDTO<Object> response = new ResponseDTO<Object>(HttpStatus.BAD_REQUEST,Constants.STATUS_FAIL,ex.getLocalizedMessage(),false);

LOGGER.error(“Data not found exception:,ex);

return new ResponseEntity<ResponseDTO<Object>>(response ,HttpStatus.BAD_REQUEST);


}

@ExceptionHandler(value = { NetworkException.class })

public ResponseEntity<ResponseDTO<Object>> networkException(Exception ex) {

ResponseDTO<Object> response= new ResponseDTO