From 2cbc7eed733372ce9dfd014d7d99857e5983b6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 30 May 2023 12:25:35 +0200 Subject: [PATCH] Always use MethodArgumentNotValidException(MethodParameter, BindingResult) constructor gh-23846 introduced a new MethodArgumentNotValidException(Executable, BindingResult) constructor that can be advantageously replaced by using MethodArgumentNotValidException(MethodParameter, BindingResult) in ModelAttributeMethodProcessor. This commit updates ModelAttributeMethodProcessor accordingly, and deprecates MethodArgumentNotValidException(Executable, BindingResult) in favor of MethodArgumentNotValidException(MethodParameter, BindingResult). Closes gh-30558 --- .../web/bind/MethodArgumentNotValidException.java | 2 ++ .../web/method/annotation/ModelAttributeMethodProcessor.java | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java b/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java index ce1c20e7dd5..8dda0674b32 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java +++ b/spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java @@ -75,7 +75,9 @@ public class MethodArgumentNotValidException extends BindException implements Er * @param executable the executable that failed validation * @param bindingResult the results of the validation * @since 6.0.5 + * @deprecated in favor of {@link #MethodArgumentNotValidException(MethodParameter, BindingResult)} */ + @Deprecated(since = "6.0.10", forRemoval = true) public MethodArgumentNotValidException(Executable executable, BindingResult bindingResult) { super(bindingResult); this.parameter = null; diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java index f80b88fad5d..2a0f45f566d 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java @@ -341,7 +341,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol BindingResult result = binder.getBindingResult(); ObjectError error = new ObjectError(ctor.getName(), cause.getMessage()); result.addError(error); - throw new MethodArgumentNotValidException(ctor, result); + throw new MethodArgumentNotValidException(parameter, result); } else { throw ex;