Skip to content

JSR-303 @Valid validation throws BindException instead of MethodArgumentNotValidException [SPR-10157] #14790

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

Closed
spring-projects-issues opened this issue Jan 9, 2013 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

Mohammad Kurabi opened SPR-10157 and commented

When using @Valid without @RequestBody on request parameter, the validation throws a BindException instead of a MethodArgumentNotValidException when the object fails validation.

Noticed this issue when I realized my global exception handler that handles MethodArgumentNotValidException was not being invoked. I beleive this should be consistent and should throw MethodArgumentNotValidException when validation fails.

Example:
https://gist.github.com/c658b7f749df09b5465e


Affects: 3.2 GA

Reference URL: https://gist.github.com/c658b7f749df09b5465e

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

These are actually intentionally different exceptions. @ModelAttribute, which is assumed by default if no other annotation is present, goes through data binding and validation, and raises BindException to indicate a failure with binding request properties or validating the resulting values. @RequestBody, on the other hand converts the body of the request via HttpMessageConverter, validates it and raises various conversion related exceptions or a MethodArgumentNotValidexception if validation fails. In most cases a MethodArgumentNotValidException can be handled generically (e.g. via @ExceptionHandler method) while BindException is very often handled individually in each controller method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants