Exception handling in an application that exposes REST endpoints is a very important task that allows us to:
In Spring it is very easy to handle exception that occurr while handling a REST request. To do that you can use AOP’s advices. Spring Web provides a ready to use annotations
@ControllerAdvice - wraps our controllers with an advice@ExceptionHandler - creates a pointcut that filters requested types of exceptions. After successful match, annotated method will be executed as an exception handler.Our job is to properly handle them and return proper data as a REST response. Below is a simple example of an exception handler that waits for any exception that occurred during the request processing and returns 500 Internal server response.
|
|