Support List<Part> method arguments

Issue: SPR-10591
This commit is contained in:
Rossen Stoyanchev
2013-07-30 22:00:37 -04:00
parent 481aeeec98
commit 803779d160
4 changed files with 82 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -17,8 +17,10 @@
package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.GenericCollectionTypeResolver;
@@ -130,8 +132,13 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
arg = multipartRequest.getFiles(partName);
}
else if ("javax.servlet.http.Part".equals(parameter.getParameterType().getName())) {
assertIsMultipartRequest(servletRequest);
arg = servletRequest.getPart(partName);
}
else if (isPartCollection(parameter)) {
assertIsMultipartRequest(servletRequest);
arg = new ArrayList(servletRequest.getParts());
}
else {
try {
HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, partName);
@@ -177,14 +184,24 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
}
private boolean isMultipartFileCollection(MethodParameter parameter) {
Class<?> collectionType = getCollectionParameterType(parameter);
return ((collectionType != null) && collectionType.equals(MultipartFile.class));
}
private boolean isPartCollection(MethodParameter parameter) {
Class<?> collectionType = getCollectionParameterType(parameter);
return ((collectionType != null) && "javax.servlet.http.Part".equals(collectionType.getName()));
}
private Class<?> getCollectionParameterType(MethodParameter parameter) {
Class<?> paramType = parameter.getParameterType();
if (Collection.class.equals(paramType) || List.class.isAssignableFrom(paramType)){
Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(parameter);
if (valueType != null && valueType.equals(MultipartFile.class)) {
return true;
if (valueType != null) {
return valueType;
}
}
return false;
return null;
}
private void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException {