ہر controller method کو try/catch میں لپیٹنے کے بجائے، Spring آپ کو error handling کو ایک class میں مرکوز کرنے دیتا ہے جس پر @ControllerAdvice (یا @RestControllerAdvice) annotation ہو۔ @ExceptionHandler سے نشان زد methods آپ کے controllers میں کہیں بھی throw کی گئی exceptions کو پکڑتی ہیں اور انہیں مستقل HTTP responses میں بدل دیتی ہیں۔
ایک global handler
{
ApiError {
(, ex.getMessage());
}
ApiError {
ex.getBindingResult().getFieldErrors().stream()
.map(e -> e.getField() + + e.getDefaultMessage())
.collect(Collectors.joining());
(, msg);
}
}
