Don't auto-configure MultipartConfigElement when using Commons FileUpload

Previously, when a user had declared a custom MultipartResolver bean
that is a CommonsMultipartResolver, part resolution would fail. The
failure was occurring as the servlet container was consuming the parts
before CommonsMultipartResolver had a chance to read them. This was
happening because a MultipartConfigElement was being auto-configured.

This commit updates the multipart auto-configuration so that a
MultipartConfigElement is not auto-configured when there is a
CommonsMultipartResolver bean in the context.

Closes gh-7735
pull/13429/merge
Andy Wilkinson 7 years ago
parent 92f62043d4
commit e7b03f7ca3

@ -662,6 +662,11 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.atomikos</groupId> <groupId>com.atomikos</groupId>
<artifactId>transactions-jms</artifactId> <artifactId>transactions-jms</artifactId>

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
@ -59,7 +60,8 @@ public class MultipartAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean({ MultipartConfigElement.class,
CommonsMultipartResolver.class })
public MultipartConfigElement multipartConfigElement() { public MultipartConfigElement multipartConfigElement() {
return this.multipartProperties.createMultipartConfig(); return this.multipartProperties.createMultipartConfig();
} }

@ -49,6 +49,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -200,6 +201,17 @@ public class MultipartAutoConfigurationTests {
.getBean(MultipartResolver.class); .getBean(MultipartResolver.class);
assertThat(multipartResolver) assertThat(multipartResolver)
.isNotInstanceOf(StandardServletMultipartResolver.class); .isNotInstanceOf(StandardServletMultipartResolver.class);
assertThat(this.context.getBeansOfType(MultipartConfigElement.class)).hasSize(1);
}
@Test
public void containerWithCommonsMultipartResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
ContainerWithCommonsMultipartResolver.class, BaseConfiguration.class);
MultipartResolver multipartResolver = this.context
.getBean(MultipartResolver.class);
assertThat(multipartResolver).isInstanceOf(CommonsMultipartResolver.class);
assertThat(this.context.getBeansOfType(MultipartConfigElement.class)).hasSize(0);
} }
@Test @Test
@ -370,6 +382,15 @@ public class MultipartAutoConfigurationTests {
} }
public static class ContainerWithCommonsMultipartResolver {
@Bean
CommonsMultipartResolver multipartResolver() {
return mock(CommonsMultipartResolver.class);
}
}
@Controller @Controller
public static class WebController { public static class WebController {

@ -54,6 +54,11 @@
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.17</version> <version>1.2.17</version>
</dependency> </dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>

Loading…
Cancel
Save