Allow Errors after @RequestBody and @RequestPart

An @RequestBody or an @RequestPart argument can now be followed by an
Errors/BindingResult argument making it possible to handle validation
errors (as a result of an @Valid annotation) locally within the
@RequestMapping method.

Issue: SPR-7114
This commit is contained in:
Rossen Stoyanchev
2012-08-21 14:23:51 -04:00
parent 06d95915a0
commit af1561634c
9 changed files with 196 additions and 112 deletions

View File

@@ -100,9 +100,8 @@ import java.util.concurrent.Callable;
* converted to the declared method argument type using
* {@linkplain org.springframework.http.converter.HttpMessageConverter message
* converters}. Such parameters may optionally be annotated with {@code @Valid}
* but do not support access to validation results through a
* {@link org.springframework.validation.Errors} /
* {@link org.springframework.validation.BindingResult} argument.
* and also support access to validation results through an
* {@link org.springframework.validation.Errors} argument.
* Instead a {@link org.springframework.web.servlet.mvc.method.annotation.MethodArgumentNotValidException}
* exception is raised.
* <li>{@link RequestPart @RequestPart} annotated parameters
@@ -112,9 +111,8 @@ import java.util.concurrent.Callable;
* converted to the declared method argument type using
* {@linkplain org.springframework.http.converter.HttpMessageConverter message
* converters}. Such parameters may optionally be annotated with {@code @Valid}
* but do not support access to validation results through a
* {@link org.springframework.validation.Errors} /
* {@link org.springframework.validation.BindingResult} argument.
* and support access to validation results through a
* {@link org.springframework.validation.Errors} argument.
* Instead a {@link org.springframework.web.servlet.mvc.method.annotation.MethodArgumentNotValidException}
* exception is raised.
* <li>{@link org.springframework.http.HttpEntity HttpEntity&lt;?&gt;} parameters

View File

@@ -60,8 +60,8 @@ public class ErrorsMethodArgumentResolver implements HandlerMethodArgumentResolv
}
throw new IllegalStateException(
"An Errors/BindingResult argument is expected to be immediately after the model attribute " +
"argument in the controller method signature: " + parameter.getMethod());
"An Errors/BindingResult argument is expected to be declared immediately after the model attribute, " +
"the @RequestBody or the @RequestPart arguments to which they apply: " + parameter.getMethod());
}
}

View File

@@ -157,16 +157,16 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
if (annot.annotationType().getSimpleName().startsWith("Valid")) {
Object hints = AnnotationUtils.getValue(annot);
binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
break;
}
}
}
/**
* Whether to raise a {@link BindException} on bind or validation errors.
* The default implementation returns {@code true} if the next method
* argument is not of type {@link Errors}.
* Whether to raise a {@link BindException} on validation errors.
* @param binder the data binder used to perform data binding
* @param parameter the method argument
* @return {@code true} if the next method argument is not of type {@link Errors}.
*/
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
int i = parameter.getParameterIndex();