Posts

Showing posts from May, 2021

Custom Exception Handling For Spring Boot Rest Controller

Image
There can be a scenario where we want to send a custom success or error message as a response to the Rest API call. This can be easily achievable using @ControllerAdvice annotation by implementing Custom Exception Handler. It can be either global error message or controller level messages. Here I am going to explain how to implement a global exception handler in spring boot. Below are the steps to implement custom error response in Spring boot Rest Controller   Implement a custom exception handler by extending ResponseEntityExceptionHandler with @ControllerAdvice annotation. Implement a Custom Exception.  Design a CustomApiException Response Model. Throw custom exception from Rest Service. Step 1 - Implementing a Custom Exception Handler When implementing a custom exception handler either you can implement a custom method to handle the exception or override the default "handleException" method  Example to implement a custom exception handler method  Below exception handler me