From e7b03f7ca357f95ed70406464694da7ff7ffdeea Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 20 Jun 2018 12:05:23 +0100 Subject: [PATCH] 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 --- spring-boot-autoconfigure/pom.xml | 5 +++++ .../web/MultipartAutoConfiguration.java | 6 ++++-- .../web/MultipartAutoConfigurationTests.java | 21 +++++++++++++++++++ spring-boot-parent/pom.xml | 5 +++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 5fa943c139..5c133d1417 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -662,6 +662,11 @@ logback-classic test + + commons-fileupload + commons-fileupload + test + com.atomikos transactions-jms diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java index 9a5d03faaa..04de3328c4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java @@ -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"); * 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.Configuration; import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.servlet.DispatcherServlet; @@ -59,7 +60,8 @@ public class MultipartAutoConfiguration { } @Bean - @ConditionalOnMissingBean + @ConditionalOnMissingBean({ MultipartConfigElement.class, + CommonsMultipartResolver.class }) public MultipartConfigElement multipartConfigElement() { return this.multipartProperties.createMultipartConfig(); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java index 403040c6ae..016652a668 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java @@ -49,6 +49,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @@ -200,6 +201,17 @@ public class MultipartAutoConfigurationTests { .getBean(MultipartResolver.class); assertThat(multipartResolver) .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 @@ -370,6 +382,15 @@ public class MultipartAutoConfigurationTests { } + public static class ContainerWithCommonsMultipartResolver { + + @Bean + CommonsMultipartResolver multipartResolver() { + return mock(CommonsMultipartResolver.class); + } + + } + @Controller public static class WebController { diff --git a/spring-boot-parent/pom.xml b/spring-boot-parent/pom.xml index 4d829cc439..7c23d7146f 100644 --- a/spring-boot-parent/pom.xml +++ b/spring-boot-parent/pom.xml @@ -54,6 +54,11 @@ log4j 1.2.17 + + commons-fileupload + commons-fileupload + 1.3.3 + com.google.guava guava