You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Spring MVC and WebFlux handling of method validation errors #30644
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.
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.
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?
Activity
Refactoring in MethodValidationResult
Polishing in Web validation exceptions
Add BindErrorUtils
Refactor to prepare for method validation handling
Refactoring in MethodValidationAdapter
Add ~.validation.method package
[-]Support handling of MethodValidationException[/-][+]Support handling of method validation errors[/+][-]Support handling of method validation errors[/-][+]Spring MVC and WebFlux handling of method validation errors[/+]rstoyanchev commentedon Jul 4, 2023
There is now a new
HandlerMethodValidationException, similar to theMethodValidationExceptionraised when method validation is applied via AOP (to any Spring component), but extending fromResponseStatusExceptionand thrown from the built-in method validation support for@RequestMappingmethods.The new exception is handled in
ResponseEntityExceptionHandlerand inDefaultHandlerExceptionResolveras 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
ProblemDetailis just a short message "Validation failure". You can customize that with aMessageSourceas described in the reference documentation where the{0}argument forHandlerMethodValidationExceptionis the list of all errors. For more control, handle this exception directly and provide it with aVisitorto handle controller method parameters individually.ctapobep commentedon Jan 23, 2024
From the documentation it's not clear - does this mean that the old
MethodArgumentNotValidExceptionisn't going to be used by Spring MVC anymore? We should always expectHandlerMethodValidationExceptionon the API level from now on?Add default web handling of method validation errors