Skip to content

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

@seung-hun-h

Description

@seung-hun-h

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions