Spring mvc validation exceptionhandler invoked before controller method
You will notice the exception is thrown at ModelAttributeMethodProcessor#resolveArgument(..)
. It happens here
if (binder.getBindingResult().hasErrors()) {
if (isBindExceptionRequired(binder, parameter)) {
throw new BindException(binder.getBindingResult());
}
}
So if there was an error parsing the date parameter (or any other error) and isBindExceptionRequest(..)
returns true, the BindingResult
is wrapped in a BindException
and thrown. So what is isBindExceptionRequires(..)
?
It's implemented as such
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
int i = parameter.getParameterIndex();
Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
return !hasBindingResult;
}
and as the Javadoc says
Returns:
true
if the next method argument is not of type Errors.
In other words, if your handler method is declared like so
public String submitForm(@Valid @ModelAttribute(MODEL_ATTRIBUTE_STUDENT) StudentDTO edited,
BindingResult bindingResult, RedirectAttributes attributes) {
a BindException
will not be thrown since there is a parameter of type Errors
(BindingResult
is a sub type) next to the @ModelAttribute
parameter.
This means that you are getting the exception from some other handler method where you don't have the BindingResult
following a command object parameter. Or there is something else in your configuration which you aren't showing us.