Use MethodArgumentNotValidException for model attributes
Closes gh-29251
This commit is contained in:
@@ -50,6 +50,7 @@ import org.springframework.validation.Errors;
|
|||||||
import org.springframework.validation.SmartValidator;
|
import org.springframework.validation.SmartValidator;
|
||||||
import org.springframework.validation.Validator;
|
import org.springframework.validation.Validator;
|
||||||
import org.springframework.validation.annotation.ValidationAnnotationUtils;
|
import org.springframework.validation.annotation.ValidationAnnotationUtils;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||||
@@ -145,7 +146,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
|||||||
try {
|
try {
|
||||||
attribute = createAttribute(name, parameter, binderFactory, webRequest);
|
attribute = createAttribute(name, parameter, binderFactory, webRequest);
|
||||||
}
|
}
|
||||||
catch (BindException ex) {
|
catch (MethodArgumentNotValidException ex) {
|
||||||
if (isBindExceptionRequired(parameter)) {
|
if (isBindExceptionRequired(parameter)) {
|
||||||
// No BindingResult parameter -> fail with BindException
|
// No BindingResult parameter -> fail with BindException
|
||||||
throw ex;
|
throw ex;
|
||||||
@@ -314,7 +315,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
|||||||
if (!parameter.isOptional()) {
|
if (!parameter.isOptional()) {
|
||||||
try {
|
try {
|
||||||
Object target = BeanUtils.instantiateClass(ctor, args);
|
Object target = BeanUtils.instantiateClass(ctor, args);
|
||||||
throw new BindException(result) {
|
throw new MethodArgumentNotValidException(parameter, result) {
|
||||||
@Override
|
@Override
|
||||||
public Object getTarget() {
|
public Object getTarget() {
|
||||||
return target;
|
return target;
|
||||||
@@ -325,7 +326,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
|||||||
// swallow and proceed without target instance
|
// swallow and proceed without target instance
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new BindException(result);
|
throw new MethodArgumentNotValidException(parameter, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return BeanUtils.instantiateClass(ctor, args);
|
return BeanUtils.instantiateClass(ctor, args);
|
||||||
|
|||||||
@@ -472,8 +472,11 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
|
|||||||
* @param request the current request
|
* @param request the current request
|
||||||
* @return a {@code ResponseEntity} for the response to use, possibly
|
* @return a {@code ResponseEntity} for the response to use, possibly
|
||||||
* {@code null} when the response is already committed
|
* {@code null} when the response is already committed
|
||||||
|
* @deprecated as of 6.0 since {@link org.springframework.web.method.annotation.ModelAttributeMethodProcessor}
|
||||||
|
* now raises the {@link MethodArgumentNotValidException} subclass instead.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@Deprecated(since = "6.0", forRemoval = true)
|
||||||
protected ResponseEntity<Object> handleBindException(
|
protected ResponseEntity<Object> handleBindException(
|
||||||
BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||||
|
|
||||||
|
|||||||
@@ -574,7 +574,10 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
|
|||||||
* @param handler the executed handler
|
* @param handler the executed handler
|
||||||
* @return an empty {@code ModelAndView} indicating the exception was handled
|
* @return an empty {@code ModelAndView} indicating the exception was handled
|
||||||
* @throws IOException potentially thrown from {@link HttpServletResponse#sendError}
|
* @throws IOException potentially thrown from {@link HttpServletResponse#sendError}
|
||||||
|
* @deprecated as of 6.0 since {@link org.springframework.web.method.annotation.ModelAttributeMethodProcessor}
|
||||||
|
* now raises the {@link MethodArgumentNotValidException} subclass instead.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated(since = "6.0", forRemoval = true)
|
||||||
protected ModelAndView handleBindException(BindException ex, HttpServletRequest request,
|
protected ModelAndView handleBindException(BindException ex, HttpServletRequest request,
|
||||||
HttpServletResponse response, @Nullable Object handler) throws IOException {
|
HttpServletResponse response, @Nullable Object handler) throws IOException {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user