16
16
17
17
package org .springframework .web .servlet .mvc .method .annotation ;
18
18
19
- import java .lang .annotation .Annotation ;
20
19
import java .util .ArrayList ;
21
20
import java .util .Collection ;
22
21
import java .util .List ;
26
25
27
26
import org .springframework .core .GenericCollectionTypeResolver ;
28
27
import org .springframework .core .MethodParameter ;
29
- import org .springframework .core .annotation .AnnotationUtils ;
30
28
import org .springframework .http .HttpInputMessage ;
31
29
import org .springframework .http .converter .HttpMessageConverter ;
32
30
import org .springframework .lang .UsesJava8 ;
33
31
import org .springframework .util .Assert ;
34
32
import org .springframework .validation .BindingResult ;
35
- import org .springframework .validation .Errors ;
36
- import org .springframework .validation .annotation .Validated ;
37
33
import org .springframework .web .bind .MethodArgumentNotValidException ;
38
34
import org .springframework .web .bind .WebDataBinder ;
39
35
import org .springframework .web .bind .annotation .RequestBody ;
@@ -86,9 +82,9 @@ public RequestPartMethodArgumentResolver(List<HttpMessageConverter<?>> messageCo
86
82
/**
87
83
* Supports the following:
88
84
* <ul>
89
- * <li>Annotated with {@code @RequestPart}
90
- * <li>Of type {@link MultipartFile} unless annotated with {@code @RequestParam}.
91
- * <li>Of type {@code javax.servlet.http.Part} unless annotated with {@code @RequestParam}.
85
+ * <li>annotated with {@code @RequestPart}
86
+ * <li>of type {@link MultipartFile} unless annotated with {@code @RequestParam}
87
+ * <li>of type {@code javax.servlet.http.Part} unless annotated with {@code @RequestParam}
92
88
* </ul>
93
89
*/
94
90
@ Override
@@ -164,7 +160,10 @@ else if (isPartArray(parameter)) {
164
160
arg = readWithMessageConverters (inputMessage , parameter , parameter .getNestedGenericParameterType ());
165
161
WebDataBinder binder = binderFactory .createBinder (request , arg , partName );
166
162
if (arg != null ) {
167
- validate (binder , parameter );
163
+ validateIfApplicable (binder , parameter );
164
+ if (binder .getBindingResult ().hasErrors () && isBindExceptionRequired (binder , parameter )) {
165
+ throw new MethodArgumentNotValidException (parameter , binder .getBindingResult ());
166
+ }
168
167
}
169
168
mavContainer .addAttribute (BindingResult .MODEL_KEY_PREFIX + partName , binder .getBindingResult ());
170
169
}
@@ -174,8 +173,8 @@ else if (isPartArray(parameter)) {
174
173
}
175
174
}
176
175
177
- RequestPart annot = parameter .getParameterAnnotation (RequestPart .class );
178
- boolean isRequired = ((annot == null || annot .required ()) && !optional );
176
+ RequestPart ann = parameter .getParameterAnnotation (RequestPart .class );
177
+ boolean isRequired = ((ann == null || ann .required ()) && !optional );
179
178
180
179
if (arg == null && isRequired ) {
181
180
throw new MissingServletRequestPartException (partName );
@@ -194,80 +193,51 @@ private static void assertIsMultipartRequest(HttpServletRequest request) {
194
193
}
195
194
}
196
195
197
- private String getPartName (MethodParameter param ) {
198
- RequestPart annot = param .getParameterAnnotation (RequestPart .class );
199
- String partName = (annot != null ? annot .value () : "" );
196
+ private String getPartName (MethodParameter methodParam ) {
197
+ RequestPart ann = methodParam .getParameterAnnotation (RequestPart .class );
198
+ String partName = (ann != null ? ann .value () : "" );
200
199
if (partName .length () == 0 ) {
201
- partName = param .getParameterName ();
200
+ partName = methodParam .getParameterName ();
202
201
if (partName == null ) {
203
202
throw new IllegalArgumentException ("Request part name for argument type [" +
204
- param .getNestedParameterType ().getName () +
203
+ methodParam .getNestedParameterType ().getName () +
205
204
"] not specified, and parameter name information not found in class file either." );
206
205
}
207
206
}
208
207
return partName ;
209
208
}
210
209
211
- private boolean isMultipartFileCollection (MethodParameter param ) {
212
- Class <?> collectionType = getCollectionParameterType (param );
213
- return ( collectionType != null && collectionType . equals ( MultipartFile .class ) );
210
+ private boolean isMultipartFileCollection (MethodParameter methodParam ) {
211
+ Class <?> collectionType = getCollectionParameterType (methodParam );
212
+ return MultipartFile .class . equals ( collectionType );
214
213
}
215
214
216
- private boolean isMultipartFileArray (MethodParameter param ) {
217
- Class <?> paramType = param .getNestedParameterType ().getComponentType ();
218
- return ( paramType != null && MultipartFile .class .equals (paramType ) );
215
+ private boolean isMultipartFileArray (MethodParameter methodParam ) {
216
+ Class <?> paramType = methodParam .getNestedParameterType ().getComponentType ();
217
+ return MultipartFile .class .equals (paramType );
219
218
}
220
219
221
- private boolean isPartCollection (MethodParameter param ) {
222
- Class <?> collectionType = getCollectionParameterType (param );
220
+ private boolean isPartCollection (MethodParameter methodParam ) {
221
+ Class <?> collectionType = getCollectionParameterType (methodParam );
223
222
return (collectionType != null && "javax.servlet.http.Part" .equals (collectionType .getName ()));
224
223
}
225
224
226
- private boolean isPartArray (MethodParameter param ) {
227
- Class <?> paramType = param .getNestedParameterType ().getComponentType ();
225
+ private boolean isPartArray (MethodParameter methodParam ) {
226
+ Class <?> paramType = methodParam .getNestedParameterType ().getComponentType ();
228
227
return (paramType != null && "javax.servlet.http.Part" .equals (paramType .getName ()));
229
228
}
230
229
231
- private Class <?> getCollectionParameterType (MethodParameter param ) {
232
- Class <?> paramType = param .getNestedParameterType ();
230
+ private Class <?> getCollectionParameterType (MethodParameter methodParam ) {
231
+ Class <?> paramType = methodParam .getNestedParameterType ();
233
232
if (Collection .class .equals (paramType ) || List .class .isAssignableFrom (paramType )){
234
- Class <?> valueType = GenericCollectionTypeResolver .getCollectionParameterType (param );
233
+ Class <?> valueType = GenericCollectionTypeResolver .getCollectionParameterType (methodParam );
235
234
if (valueType != null ) {
236
235
return valueType ;
237
236
}
238
237
}
239
238
return null ;
240
239
}
241
240
242
- /**
243
- * Validate the request part if applicable.
244
- * <p>The default implementation checks for {@code @javax.validation.Valid},
245
- * Spring's {@link org.springframework.validation.annotation.Validated},
246
- * and custom annotations whose name starts with "Valid".
247
- * @param binder the DataBinder to be used
248
- * @param param the method parameter
249
- * @throws MethodArgumentNotValidException in case of a binding error which
250
- * is meant to be fatal (i.e. without a declared {@link Errors} parameter)
251
- * @see #isBindingErrorFatal
252
- */
253
- protected void validate (WebDataBinder binder , MethodParameter param ) throws MethodArgumentNotValidException {
254
- Annotation [] annotations = param .getParameterAnnotations ();
255
- for (Annotation ann : annotations ) {
256
- Validated validatedAnn = AnnotationUtils .getAnnotation (ann , Validated .class );
257
- if (validatedAnn != null || ann .annotationType ().getSimpleName ().startsWith ("Valid" )) {
258
- Object hints = (validatedAnn != null ? validatedAnn .value () : AnnotationUtils .getValue (ann ));
259
- Object [] validationHints = (hints instanceof Object [] ? (Object []) hints : new Object [] {hints });
260
- binder .validate (validationHints );
261
- BindingResult bindingResult = binder .getBindingResult ();
262
- if (bindingResult .hasErrors ()) {
263
- if (isBindingErrorFatal (param )) {
264
- throw new MethodArgumentNotValidException (param , bindingResult );
265
- }
266
- }
267
- }
268
- }
269
- }
270
-
271
241
272
242
/**
273
243
* Inner class to avoid hard-coded dependency on Servlet 3.0 Part type...
0 commit comments