Use `@ControllerAdvice` with `@ExceptionHandler` to map exceptions to consistent HTTP responses (status + body). It keeps controllers clean and centralizes error handling.
@ControllerAdvice
class ApiErrors {
@ExceptionHandler(IllegalArgumentException.class)
ResponseEntity<String> badRequest(IllegalArgumentException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}