@RestControllerAdvice is a specialization of @ControllerAdvice annotation which is implemented to handle the exceptions thrown by the @RestController.
It allows us to handle exceptions across the whole application in a global, consistent manner.
By default, @RestControllerAdvice will apply globally to all controllers. We can narrow the subset of applicable by specifying annotations, packages, or controller base types in the @RestControllerAdvice annotation.
It allows full control over the body of the response as well as the status code.
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
log.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
@RestControllerAdvice
is a specialization of@ControllerAdvice
annotation which is implemented to handle the exceptions thrown by the@RestController
. It allows us to handle exceptions across the whole application in a global, consistent manner.@RestControllerAdvice
will apply globally to all controllers. We can narrow the subset of applicable by specifying annotations, packages, or controller base types in the@RestControllerAdvice
annotation.