Instead of wrapping every controller method in try/catch, Spring lets you centralize error handling in one class annotated @ControllerAdvice (or @RestControllerAdvice). Methods marked @ExceptionHandler catch exceptions thrown anywhere in your controllers and turn them into consistent HTTP responses.
A global handler
{
ApiError {
(, ex.getMessage());
}
ApiError {
ex.getBindingResult().getFieldErrors().stream()
.map(e -> e.getField() + + e.getDefaultMessage())
.collect(Collectors.joining());
(, msg);
}
}
