spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Custom `@ExceptionHandler` silently ignored when `spring.mvc.problemdetails.enabled=true` without `@Order`

Open seung-hun-h opened this issue 3 days ago • 0 comments

When spring.mvc.problemdetails.enabled=true, I want to customize the response for MethodArgumentNotValidException using @ExceptionHandler in my @RestControllerAdvicewithout 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

  1. Clearly document that @Order is required when customizing exceptions already handled by ResponseEntityExceptionHandler with spring.mvc.problemdetails.enabled=true
  2. Make user-defined @ExceptionHandler methods take precedence over framework-provided handlers by default

Environment

  • Spring Boot: 3.5.8
  • Spring Framework: 6.2.14
  • Java: 21

Sample Project

https://github.com/seung-hun-h/spring-error-response

seung-hun-h avatar Dec 09 '25 09:12 seung-hun-h