Browse Source

8483 Add support for MultipartFile arg type in RequestPartMethodArgumentResolver

pull/7/head
Rossen Stoyanchev 14 years ago
parent
commit
e0d2e20fc4
  1. 6
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolver.java
  2. 16
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java

6
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolver.java

@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.RequestPart; @@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.multipart.RequestPartServletServerHttpRequest;
@ -77,8 +78,11 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM @@ -77,8 +78,11 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
}
String partName = getPartName(parameter);
if (MultipartFile.class.isAssignableFrom(parameter.getParameterType())) {
return multipartRequest.getFile(partName);
}
HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(multipartRequest, partName);
Object arg = readWithMessageConverters(inputMessage, parameter, parameter.getParameterType());
if (isValidationApplicable(arg, parameter)) {

16
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java

@ -23,6 +23,7 @@ import static org.easymock.EasyMock.isA; @@ -23,6 +23,7 @@ import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -71,6 +72,7 @@ public class RequestPartMethodArgumentResolverTests { @@ -71,6 +72,7 @@ public class RequestPartMethodArgumentResolverTests {
private MethodParameter paramRequestPart;
private MethodParameter paramNamedRequestPart;
private MethodParameter paramValidRequestPart;
private MethodParameter paramMultipartFile;
private MethodParameter paramInt;
private NativeWebRequest webRequest;
@ -82,12 +84,14 @@ public class RequestPartMethodArgumentResolverTests { @@ -82,12 +84,14 @@ public class RequestPartMethodArgumentResolverTests {
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
Method handle = getClass().getMethod("handle", SimpleBean.class, SimpleBean.class, SimpleBean.class, Integer.TYPE);
Method handle = getClass().getMethod("handle",
SimpleBean.class, SimpleBean.class, SimpleBean.class, MultipartFile.class, Integer.TYPE);
paramRequestPart = new MethodParameter(handle, 0);
paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
paramNamedRequestPart = new MethodParameter(handle, 1);
paramValidRequestPart = new MethodParameter(handle, 2);
paramInt = new MethodParameter(handle, 3);
paramMultipartFile = new MethodParameter(handle, 3);
paramInt = new MethodParameter(handle, 4);
messageConverter = createMock(HttpMessageConverter.class);
expect(messageConverter.getSupportedMediaTypes()).andReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
@ -119,6 +123,13 @@ public class RequestPartMethodArgumentResolverTests { @@ -119,6 +123,13 @@ public class RequestPartMethodArgumentResolverTests {
testResolveArgument(new SimpleBean("foo"), paramNamedRequestPart);
}
@Test
public void resolveMultipartFile() throws Exception {
Object actual = resolver.resolveArgument(paramMultipartFile, null, webRequest, null);
assertNotNull(actual);
assertSame(multipartFile, actual);
}
@Test
public void resolveRequestPartNotValid() throws Exception {
try {
@ -156,6 +167,7 @@ public class RequestPartMethodArgumentResolverTests { @@ -156,6 +167,7 @@ public class RequestPartMethodArgumentResolverTests {
public void handle(@RequestPart SimpleBean requestPart,
@RequestPart("requestPart") SimpleBean namedRequestPart,
@Valid @RequestPart("requestPart") SimpleBean validRequestPart,
@RequestPart("requestPart") MultipartFile multipartFile,
int i) {
}

Loading…
Cancel
Save