Skip to content

Spring MVC and WebFlux handling of method validation errors #30644

Closed

Description

@rstoyanchev

The new MethodValidationException introduced as part of #29825 needs to have default handling in Spring MVC and WebFlux, including RFC 7807 support, similar to the handling of BindException but reflecting errors for multiple method parameters / or return value, represented with ParameterValidationResult and ParameterErrors.

Activity

  1. added
    in: webIssues in web modules (web, webmvc, webflux, websocket)
    on Jun 12, 2023
  2. added this to the 6.1.0-M2 milestone on Jun 12, 2023
  3. self-assigned this
    on Jun 12, 2023
  4. changed the title [-]Support handling of MethodValidationException[/-] [+]Support handling of method validation errors[/+] on Jul 4, 2023
  5. changed the title [-]Support handling of method validation errors[/-] [+]Spring MVC and WebFlux handling of method validation errors[/+] on Jul 4, 2023
  6. rstoyanchev commented on Jul 4, 2023

    @rstoyanchev
    ContributorAuthor

    There is now a new HandlerMethodValidationException, similar to the MethodValidationException raised when method validation is applied via AOP (to any Spring component), but extending from ResponseStatusException and thrown from the built-in method validation support for @RequestMapping methods.

    The new exception is handled in ResponseEntityExceptionHandler and in DefaultHandlerExceptionResolver
    as e 400 error if is for method arguments, and as a 500 error if it is for a return value. MethodValidationException (possibly from an underlying service) is handled as a 500 error.

    By default for RFC 7807, the detail of ProblemDetail is just a short message "Validation failure". You can customize that with a MessageSource as described in the reference documentation where the {0} argument for HandlerMethodValidationException is the list of all errors. For more control, handle this exception directly and provide it with a Visitor to handle controller method parameters individually.

  7. ctapobep commented on Jan 23, 2024

    @ctapobep

    From the documentation it's not clear - does this mean that the old MethodArgumentNotValidException isn't going to be used by Spring MVC anymore? We should always expect HandlerMethodValidationException on the API level from now on?

  8. added a commit that references this issue on Sep 9, 2024
    7a79da5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions