Spring w praktyce: ResponseEntityExceptionHandler
W przypadku gdy nasze ziarno opisane jako @ControllerAdvice dziedziczy po ResponseEntityExceptionHandler automatycznie załadowane zostaną definicje @ExceptionHandler z klasy ResponseEntityExceptionHandler.
Co więcej, możemy nadpisywać metody z ResponseEntityExceptionHandler obsługujące dany błąd.
@Override
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
HttpRequestMethodNotSupportedException ex, HttpHeaders headers,
HttpStatusCode status, WebRequest request) {
super.handleHttpRequestMethodNotSupported(ex, headers, status, request);
return new ResponseEntity<>(
ErrorDto.builder().message("Method not allowed").build(),
HttpStatus.METHOD_NOT_ALLOWED);
}