Użyj `@ControllerAdvice` z `@ExceptionHandler`, aby mapować wyjątki na spójne odpowiedzi HTTP (status + body). Dzięki temu kontrolery są czystsze, a obsługa błędów jest w jednym miejscu.
@ControllerAdvice
class ApiErrors {
@ExceptionHandler(IllegalArgumentException.class)
ResponseEntity<String> badRequest(IllegalArgumentException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}