@RestControllerAdvice et @ExceptionHandler

@RestControllerAdvice centralizes the exception handling of all controllers in one class. Each @ExceptionHandler method maps an exception type to an HTTP response (404, 409, 400). Controllers and services simply throw business exceptions, which are translated in one place.

Example

@ExceptionHandler(ClientIntrouvable.class)
ResponseEntity<?> introuvable(ClientIntrouvable e) {
    return ResponseEntity.notFound().build();
}

Related concepts

The whole glossary