Use MethodArgumentNotValidException for model attributes

Closes gh-29251
This commit is contained in:
rstoyanchev
2022-10-05 14:01:05 +01:00
parent 6b0f29a065
commit b08f185a0b
3 changed files with 10 additions and 3 deletions

View File

@@ -50,6 +50,7 @@ import org.springframework.validation.Errors;
import org.springframework.validation.SmartValidator;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.ValidationAnnotationUtils;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.support.WebDataBinderFactory;
@@ -145,7 +146,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
try {
attribute = createAttribute(name, parameter, binderFactory, webRequest);
}
catch (BindException ex) {
catch (MethodArgumentNotValidException ex) {
if (isBindExceptionRequired(parameter)) {
// No BindingResult parameter -> fail with BindException
throw ex;
@@ -314,7 +315,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
if (!parameter.isOptional()) {
try {
Object target = BeanUtils.instantiateClass(ctor, args);
throw new BindException(result) {
throw new MethodArgumentNotValidException(parameter, result) {
@Override
public Object getTarget() {
return target;
@@ -325,7 +326,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
// swallow and proceed without target instance
}
}
throw new BindException(result);
throw new MethodArgumentNotValidException(parameter, result);
}
return BeanUtils.instantiateClass(ctor, args);