Skip to content

Commit 25644db

Browse files
committed
Revised validation javadoc, plus protected validate/isBindingErrorFatal template methods
Issue: SPR-12655 (cherry picked from commit 7585be8)
1 parent 181b352 commit 25644db

File tree

5 files changed

+60
-37
lines changed

5 files changed

+60
-37
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -144,6 +144,16 @@ else if (payload instanceof String) {
144144
}
145145
}
146146

147+
/**
148+
* Validate the payload if applicable.
149+
* <p>The default implementation checks for {@code @javax.validation.Valid},
150+
* Spring's {@link org.springframework.validation.annotation.Validated},
151+
* and custom annotations whose name starts with "Valid".
152+
* @param message the currently processed message
153+
* @param parameter the method parameter
154+
* @param target the target payload object
155+
* @throws MethodArgumentNotValidException in case of binding errors
156+
*/
147157
protected void validate(Message<?> message, MethodParameter parameter, Object target) {
148158
if (this.validator == null) {
149159
return;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@
5656
*/
5757
public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
5858

59-
protected Log logger = LogFactory.getLog(this.getClass());
59+
protected final Log logger = LogFactory.getLog(getClass());
6060

6161
private final boolean annotationNotRequired;
6262

@@ -72,8 +72,8 @@ public ModelAttributeMethodProcessor(boolean annotationNotRequired) {
7272

7373

7474
/**
75-
* @return true if the parameter is annotated with {@link ModelAttribute}
76-
* or in default resolution mode also if it is not a simple type.
75+
* Returns {@code true} if the parameter is annotated with {@link ModelAttribute}
76+
* or in default resolution mode, and also if it is not a simple type.
7777
*/
7878
@Override
7979
public boolean supportsParameter(MethodParameter parameter) {
@@ -151,7 +151,9 @@ protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest requ
151151

152152
/**
153153
* Validate the model attribute if applicable.
154-
* <p>The default implementation checks for {@code @javax.validation.Valid}.
154+
* <p>The default implementation checks for {@code @javax.validation.Valid},
155+
* Spring's {@link org.springframework.validation.annotation.Validated},
156+
* and custom annotations whose name starts with "Valid".
155157
* @param binder the DataBinder to be used
156158
* @param parameter the method parameter
157159
*/

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import org.springframework.http.converter.HttpMessageConverter;
3838
import org.springframework.http.server.ServletServerHttpRequest;
3939
import org.springframework.util.Assert;
40+
import org.springframework.validation.Errors;
4041
import org.springframework.web.HttpMediaTypeNotSupportedException;
4142
import org.springframework.web.context.request.NativeWebRequest;
4243
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@@ -79,6 +80,7 @@ private static List<MediaType> getAllSupportedMediaTypes(List<HttpMessageConvert
7980
return Collections.unmodifiableList(result);
8081
}
8182

83+
8284
/**
8385
* Create the method argument value of the expected parameter type by
8486
* reading from the given request.
@@ -162,4 +164,17 @@ protected ServletServerHttpRequest createInputMessage(NativeWebRequest webReques
162164
return new ServletServerHttpRequest(servletRequest);
163165
}
164166

167+
/**
168+
* Whether to raise a handler method invocation exception on validation errors.
169+
* @param parameter the method argument
170+
* @return {@code true} if the next method argument is not of type {@link Errors}
171+
* @since 4.1.5
172+
*/
173+
protected boolean isBindingErrorFatal(MethodParameter parameter) {
174+
int i = parameter.getParameterIndex();
175+
Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
176+
boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
177+
return !hasBindingResult;
178+
}
179+
165180
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -222,7 +222,18 @@ private Class<?> getCollectionParameterType(MethodParameter parameter) {
222222
return null;
223223
}
224224

225-
private void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException {
225+
/**
226+
* Validate the request part if applicable.
227+
* <p>The default implementation checks for {@code @javax.validation.Valid},
228+
* Spring's {@link org.springframework.validation.annotation.Validated},
229+
* and custom annotations whose name starts with "Valid".
230+
* @param binder the DataBinder to be used
231+
* @param parameter the method parameter
232+
* @throws MethodArgumentNotValidException in case of a binding error which
233+
* is meant to be fatal (i.e. without a declared {@link Errors} parameter)
234+
* @see #isBindingErrorFatal
235+
*/
236+
protected void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException {
226237
Annotation[] annotations = parameter.getParameterAnnotations();
227238
for (Annotation ann : annotations) {
228239
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
@@ -240,18 +251,6 @@ private void validate(WebDataBinder binder, MethodParameter parameter) throws Me
240251
}
241252
}
242253

243-
/**
244-
* Whether to raise a {@link MethodArgumentNotValidException} on validation errors.
245-
* @param parameter the method argument
246-
* @return {@code true} if the next method argument is not of type {@link Errors}
247-
*/
248-
private boolean isBindingErrorFatal(MethodParameter parameter) {
249-
int i = parameter.getParameterIndex();
250-
Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
251-
boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
252-
return !hasBindingResult;
253-
}
254-
255254

256255
/**
257256
* Inner class to avoid hard-coded dependency on Servlet 3.0 Part type...

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,18 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
112112
return argument;
113113
}
114114

115-
private void validate(WebDataBinder binder, MethodParameter parameter) throws Exception {
115+
/**
116+
* Validate the request part if applicable.
117+
* <p>The default implementation checks for {@code @javax.validation.Valid},
118+
* Spring's {@link org.springframework.validation.annotation.Validated},
119+
* and custom annotations whose name starts with "Valid".
120+
* @param binder the DataBinder to be used
121+
* @param parameter the method parameter
122+
* @throws MethodArgumentNotValidException in case of a binding error which
123+
* is meant to be fatal (i.e. without a declared {@link Errors} parameter)
124+
* @see #isBindingErrorFatal
125+
*/
126+
protected void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException {
116127
Annotation[] annotations = parameter.getParameterAnnotations();
117128
for (Annotation ann : annotations) {
118129
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
@@ -122,7 +133,7 @@ private void validate(WebDataBinder binder, MethodParameter parameter) throws Ex
122133
binder.validate(validationHints);
123134
BindingResult bindingResult = binder.getBindingResult();
124135
if (bindingResult.hasErrors()) {
125-
if (isBindExceptionRequired(binder, parameter)) {
136+
if (isBindingErrorFatal(parameter)) {
126137
throw new MethodArgumentNotValidException(parameter, bindingResult);
127138
}
128139
}
@@ -131,20 +142,6 @@ private void validate(WebDataBinder binder, MethodParameter parameter) throws Ex
131142
}
132143
}
133144

134-
/**
135-
* Whether to raise a {@link MethodArgumentNotValidException} on validation errors.
136-
* @param binder the data binder used to perform data binding
137-
* @param parameter the method argument
138-
* @return {@code true} if the next method argument is not of type {@link Errors}.
139-
*/
140-
private boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
141-
int i = parameter.getParameterIndex();
142-
Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
143-
boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
144-
145-
return !hasBindingResult;
146-
}
147-
148145
@Override
149146
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest,
150147
MethodParameter methodParam, Type paramType) throws IOException, HttpMediaTypeNotSupportedException {

0 commit comments

Comments
 (0)