દરેક controller method ને try/catch માં લપેટવાને બદલે, Spring તમને @ControllerAdvice (અથવા @RestControllerAdvice) એનોટેટ કરેલા એક class માં error handling કેન્દ્રિત કરવા દે છે. @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);
}
}
