प्रत्येक controller method try/catch मध्ये गुंडाळण्याऐवजी, Spring तुम्हाला error handling एका @ControllerAdvice (किंवा @RestControllerAdvice) ने annotate केलेल्या class मध्ये केंद्रित करू देते. @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);
}
}
