MethodArgumentNotValidException
is thrown instead of HandlerMethodValidationException
for @Valid @RequestBody
#31775
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: invalid
An issue that we don't feel is valid
Environment
Spring Boot: 3.2.0
Spring: 6.1
Java: 21
Expected Behavior:
If a controller method parameter is invalid, a
HandlerMethodValidationException
should be thrown.Observed Behavior:
If you attach a
@Valid
annotation to a parameter, aMethodArgumentNotValidException
is thrown instead of aHandlerMethodValidationException
.Reproduce the error
Spring-initializer
Custom ExceptionHandler
REST Controller
Make request
Debugging Insights
In
org.springframework.web.method.support.InvocableHandlerMethod
, methodinvokeForRequest(...)
:The first line,
Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
throws a
MethodArgumentNotValidException
right before the handler validation should be started:if (shouldValidateArguments() && this.methodValidator != null) {
,where
methodValidator
isorg.springframework.web.method.annotation.HandlerMethodValidator
↓
↓
MethodArgumentNotValidException
is thrown byorg.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor
in methodresolveArgument(...)
:Additionally
I also tried with
@Validated
.I assume that the validation of controller method parameters, which currently throws exception
MethodArgumentNotValidException
, is deprecated and should be replaced. When is the new validation method introduced in version 6.1 expected to replace it? I might be mistaken in my understanding.The text was updated successfully, but these errors were encountered: