-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Turn MethodArgumentNotValidException into subclass of BindException #23107
Comments
I've transferred this issue to the |
There is also the exemple : This forces to handle @ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> onMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return e.getBindingResult().getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
}
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> onBindException(BindException e) {
return e.getBindingResult().getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
} a |
I was going to say you can handle both exceptions at the same time as follows... @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, String> onValidationFailureException(Exception ex) {
return ((CommonBindingResultInterface) ex).getBindingResult().getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
} ... but then I realized that So one option to make this easier would be to introduce a common interface (like the |
@rafafael03 I've read your description several times and I can't be sure I understand what you mean and there are claims that aren't true. First you can use either What @ah1508 has written is correct. Validation through either of these annotations is supported for Is this the same as your point? As for finding a way to consolidate, perhaps |
Indeed, if |
Tentatively slated for 5.3 for pending design work and assessment of viability regarding the proposal to have |
When creating a REST controller with Spring Boot it's possible to use Bean Validation to validate the methods arguments annotating the class with
@Validated
.Then Bean Validation annotations can be used directly on the argument or, if the argument is a complex class with properties that must be validated (such as the request body), using
@Valid
.The inconsistency I found was while handling violation on those validations.
While using the annotations from
javax.validation.constraints
(the validations from the Bean Validation API) the violations are handled by the@Validated
handler (MethodValidationInterceptor
) and throwsConstraintViolationException
.When using
@Valid
the violation is handled byModelAttributeMethodProcessor
and throwsMethodArgumentNotValidException
.See the problem?
The
@Validated
, a Spring annotation that says it is a "Variant of JSR-303's Valid" and "Designed for convenient use with Spring's JSR-303 support but not JSR-303 specific" (see it here), throwsConstraintViolationException
, an exception from the Bean Validation API (JSR-303).And the opposite also is true, the
@Valid
(from Bean Validation API) throwsMethodArgumentNotValidException
(Spring exception).I think it would be more concise if their behavior changed between them.
Does it makes sense?
The text was updated successfully, but these errors were encountered: