diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java index 7b5f17892cc..7bb9b865154 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -144,6 +144,16 @@ public class PayloadArgumentResolver implements HandlerMethodArgumentResolver { } } + /** + * Validate the payload if applicable. + *
The default implementation checks for {@code @javax.validation.Valid}, + * Spring's {@link org.springframework.validation.annotation.Validated}, + * and custom annotations whose name starts with "Valid". + * @param message the currently processed message + * @param parameter the method parameter + * @param target the target payload object + * @throws MethodArgumentNotValidException in case of binding errors + */ protected void validate(Message> message, MethodParameter parameter, Object target) { if (this.validator == null) { return; 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 be7b0b54edf..79d56fe5c60 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,7 +56,7 @@ import org.springframework.web.method.support.ModelAndViewContainer; */ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler { - protected Log logger = LogFactory.getLog(this.getClass()); + protected final Log logger = LogFactory.getLog(getClass()); private final boolean annotationNotRequired; @@ -72,8 +72,8 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol /** - * @return true if the parameter is annotated with {@link ModelAttribute} - * or in default resolution mode also if it is not a simple type. + * Returns {@code true} if the parameter is annotated with {@link ModelAttribute} + * or in default resolution mode, and also if it is not a simple type. */ @Override public boolean supportsParameter(MethodParameter parameter) { @@ -151,7 +151,9 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol /** * Validate the model attribute if applicable. - *
The default implementation checks for {@code @javax.validation.Valid}. + *
The default implementation checks for {@code @javax.validation.Valid}, + * Spring's {@link org.springframework.validation.annotation.Validated}, + * and custom annotations whose name starts with "Valid". * @param binder the DataBinder to be used * @param parameter the method parameter */ diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java index 49f54478bf0..28155152190 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,6 +37,7 @@ import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.util.Assert; +import org.springframework.validation.Errors; import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; @@ -79,6 +80,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements return Collections.unmodifiableList(result); } + /** * Create the method argument value of the expected parameter type by * reading from the given request. @@ -162,4 +164,17 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements return new ServletServerHttpRequest(servletRequest); } + /** + * Whether to raise a handler method invocation exception on validation errors. + * @param parameter the method argument + * @return {@code true} if the next method argument is not of type {@link Errors} + * @since 4.1.5 + */ + protected boolean isBindingErrorFatal(MethodParameter parameter) { + int i = parameter.getParameterIndex(); + Class>[] paramTypes = parameter.getMethod().getParameterTypes(); + boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1])); + return !hasBindingResult; + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java index 5d076b2e797..4a5f15c9a4c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -222,7 +222,18 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM return null; } - private void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException { + /** + * Validate the request part if applicable. + *
The default implementation checks for {@code @javax.validation.Valid}, + * Spring's {@link org.springframework.validation.annotation.Validated}, + * and custom annotations whose name starts with "Valid". + * @param binder the DataBinder to be used + * @param parameter the method parameter + * @throws MethodArgumentNotValidException in case of a binding error which + * is meant to be fatal (i.e. without a declared {@link Errors} parameter) + * @see #isBindingErrorFatal + */ + protected void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException { Annotation[] annotations = parameter.getParameterAnnotations(); for (Annotation ann : annotations) { Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); @@ -240,18 +251,6 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM } } - /** - * Whether to raise a {@link MethodArgumentNotValidException} on validation errors. - * @param parameter the method argument - * @return {@code true} if the next method argument is not of type {@link Errors} - */ - private boolean isBindingErrorFatal(MethodParameter parameter) { - int i = parameter.getParameterIndex(); - Class>[] paramTypes = parameter.getMethod().getParameterTypes(); - boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1])); - return !hasBindingResult; - } - /** * Inner class to avoid hard-coded dependency on Servlet 3.0 Part type... diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessor.java index c13e8f1a7c1..3f578a6af20 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessor.java @@ -112,7 +112,18 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter return argument; } - private void validate(WebDataBinder binder, MethodParameter parameter) throws Exception { + /** + * Validate the request part if applicable. + *
The default implementation checks for {@code @javax.validation.Valid},
+ * Spring's {@link org.springframework.validation.annotation.Validated},
+ * and custom annotations whose name starts with "Valid".
+ * @param binder the DataBinder to be used
+ * @param parameter the method parameter
+ * @throws MethodArgumentNotValidException in case of a binding error which
+ * is meant to be fatal (i.e. without a declared {@link Errors} parameter)
+ * @see #isBindingErrorFatal
+ */
+ protected void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException {
Annotation[] annotations = parameter.getParameterAnnotations();
for (Annotation ann : annotations) {
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
@@ -122,7 +133,7 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
binder.validate(validationHints);
BindingResult bindingResult = binder.getBindingResult();
if (bindingResult.hasErrors()) {
- if (isBindExceptionRequired(binder, parameter)) {
+ if (isBindingErrorFatal(parameter)) {
throw new MethodArgumentNotValidException(parameter, bindingResult);
}
}
@@ -131,20 +142,6 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
}
}
- /**
- * Whether to raise a {@link MethodArgumentNotValidException} 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}.
- */
- private boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
- int i = parameter.getParameterIndex();
- Class>[] paramTypes = parameter.getMethod().getParameterTypes();
- boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
-
- return !hasBindingResult;
- }
-
@Override
protected