Spring 4 Java Configuration for MultipartResolver for Servlet 3.0

I use the all-Java approach to configuring Spring MVC and cannot figure out how to programmatically associate MultipartConfigElement with my DispatcherServlet .

Spring documentation reads:

To use multiparameter parsing based on Servlet 3.0, you need to mark the DispatcherServlet with the "multipart-config" section in web.xml or with javax.servlet.MultipartConfigElement in the registration servlet ...

http://docs.spring.io/spring/docs/4.0.4.RELEASE/spring-framework-reference/htmlsingle/#mvc-multipart

Here is my WebApplicationInitializer code:

 public class DispatcherServletInitializer implements WebApplicationInitializer { private static final Logger logger = LoggerFactory.getLogger(DispatcherServletInitializer.class); @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(WebConfig.class); //HOW CAN I ASSOCIATE THIS CONFIG WITH MY DISPATCHER SERVLET? MultipartConfigElement config = new MultipartConfigElement("C:\\Temp", 20848820, 418018841, 1048576); DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", dispatcherServlet); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); } } 

How do I associate a MultipartConfigElement with my DispatcherServlet ? I don't see any method like setMultipartConfiguration or any constructor that accepts it.

Also note that my WebConfig declares MultipartResolver :

 @Bean public StandardServletMultipartResolver multipartResolver(){ return new StandardServletMultipartResolver(); } 

But Spring documentation says:

 Configuration settings such as maximum sizes or storage locations need to be applied at that Servlet registration level as Servlet 3.0 does not allow for those settings to be done from the MultipartResolver. 

Everyone is welcome any guide.

+8
java spring spring-java-config spring-mvc servlets
source share
2 answers

It looks like you need this:

 ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", dispatcherServlet); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); dispatcher.setMultipartConfig(new MultipartConfigElement("/tmp", 1024*1024*5, 1024*1024*5*5, 1024*1024)); 
+11
source share

Here is a solution compatible with the AbstractAnnotationConfigDispatcherServletInitializer way of configuring a servlet. This is slightly less invasive than WebApplicationInitializer .

It uses an override of AbstractAnnotationConfigDispatcherServletInitializer.customizeRegistration .

 public class MySpringWebSetup extends AbstractAnnotationConfigDispatcherServletInitializer { // Your usual obligatory configuration overrides: @Override protected Class<?>[] getRootConfigClasses() { ... } @Override protected Class<?>[] getServletConfigClasses() { ... } @Override protected String[] getServletMappings() { ... } // Optional configuration: @Override protected void customizeRegistration(Dynamic registration) { registration.setMultipartConfig( // Maybe use more sophisticated configuration than this: new MultipartConfigElement("") ); } } 

I found that he caught the getServletMappings stack getServletMappings and thus ended up in the code org\springframework\web\servlet\support\AbstractDispatcherServletInitializer.java :

 protected void registerDispatcherServlet(ServletContext servletContext) { [more registration stuff was here] registration.setLoadOnStartup(1); registration.addMapping(getServletMappings()); registration.setAsyncSupported(isAsyncSupported()); Filter[] filters = getServletFilters(); if (!ObjectUtils.isEmpty(filters)) { for (Filter filter : filters) { registerServletFilter(servletContext, filter); } } customizeRegistration(registration); } 
+1
source share

All Articles