-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Open
Labels
status: waiting-for-triageAn issue we've not yet triaged or decided onAn issue we've not yet triaged or decided on
Description
When spring.mvc.problemdetails.enabled=true, I want to customize the response for MethodArgumentNotValidException using @ExceptionHandler in my @RestControllerAdvice — without extending ResponseEntityExceptionHandler.
However, my custom handler is silently ignored with no warning or error. The built-in ProblemDetailsExceptionHandler handles the exception instead.
Current Behavior
@RestControllerAdvice
public class GlobalExceptionHandler {
// ❌ This does NOT work — silently ignored
@ExceptionHandler(MethodArgumentNotValidException.class)
public ProblemDetail handle(MethodArgumentNotValidException ex) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(
HttpStatus.BAD_REQUEST, "Custom validation error message");
problemDetail.setProperty("errors", extractFieldErrors(ex));
return problemDetail;
}
}Expected: Custom handler is invoked
Actual: ProblemDetailsExceptionHandler handles it, custom handler is ignored
Workaround
Adding @Order(Ordered.HIGHEST_PRECEDENCE) makes it work:
@Order(Ordered.HIGHEST_PRECEDENCE) // Required
@RestControllerAdvice
public class GlobalExceptionHandler {
// Now it works
}Alternatively, extending ResponseEntityExceptionHandler and overriding methods also works — but this forces inheritance when I only want to customize a single exception.
Suggestion
- Clearly document that
@Orderis required when customizing exceptions already handled byResponseEntityExceptionHandlerwithspring.mvc.problemdetails.enabled=true - Make user-defined
@ExceptionHandlermethods take precedence over framework-provided handlers by default
Environment
- Spring Boot: 3.5.8
- Spring Framework: 6.2.14
- Java: 21
Sample Project
Metadata
Metadata
Assignees
Labels
status: waiting-for-triageAn issue we've not yet triaged or decided onAn issue we've not yet triaged or decided on