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