Skip to content

Commit b08f185

Browse files
committed
Use MethodArgumentNotValidException for model attributes
Closes gh-29251
1 parent 6b0f29a commit b08f185

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.validation.SmartValidator;
5151
import org.springframework.validation.Validator;
5252
import org.springframework.validation.annotation.ValidationAnnotationUtils;
53+
import org.springframework.web.bind.MethodArgumentNotValidException;
5354
import org.springframework.web.bind.WebDataBinder;
5455
import org.springframework.web.bind.annotation.ModelAttribute;
5556
import org.springframework.web.bind.support.WebDataBinderFactory;
@@ -145,7 +146,7 @@ public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAn
145146
try {
146147
attribute = createAttribute(name, parameter, binderFactory, webRequest);
147148
}
148-
catch (BindException ex) {
149+
catch (MethodArgumentNotValidException ex) {
149150
if (isBindExceptionRequired(parameter)) {
150151
// No BindingResult parameter -> fail with BindException
151152
throw ex;
@@ -314,7 +315,7 @@ protected Object constructAttribute(Constructor<?> ctor, String attributeName, M
314315
if (!parameter.isOptional()) {
315316
try {
316317
Object target = BeanUtils.instantiateClass(ctor, args);
317-
throw new BindException(result) {
318+
throw new MethodArgumentNotValidException(parameter, result) {
318319
@Override
319320
public Object getTarget() {
320321
return target;
@@ -325,7 +326,7 @@ public Object getTarget() {
325326
// swallow and proceed without target instance
326327
}
327328
}
328-
throw new BindException(result);
329+
throw new MethodArgumentNotValidException(parameter, result);
329330
}
330331

331332
return BeanUtils.instantiateClass(ctor, args);

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,11 @@ protected ResponseEntity<Object> handleHttpMessageNotWritable(
472472
* @param request the current request
473473
* @return a {@code ResponseEntity} for the response to use, possibly
474474
* {@code null} when the response is already committed
475+
* @deprecated as of 6.0 since {@link org.springframework.web.method.annotation.ModelAttributeMethodProcessor}
476+
* now raises the {@link MethodArgumentNotValidException} subclass instead.
475477
*/
476478
@Nullable
479+
@Deprecated(since = "6.0", forRemoval = true)
477480
protected ResponseEntity<Object> handleBindException(
478481
BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
479482

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,10 @@ protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableExcept
574574
* @param handler the executed handler
575575
* @return an empty {@code ModelAndView} indicating the exception was handled
576576
* @throws IOException potentially thrown from {@link HttpServletResponse#sendError}
577+
* @deprecated as of 6.0 since {@link org.springframework.web.method.annotation.ModelAttributeMethodProcessor}
578+
* now raises the {@link MethodArgumentNotValidException} subclass instead.
577579
*/
580+
@Deprecated(since = "6.0", forRemoval = true)
578581
protected ModelAndView handleBindException(BindException ex, HttpServletRequest request,
579582
HttpServletResponse response, @Nullable Object handler) throws IOException {
580583

0 commit comments

Comments
 (0)