I\'m taking an all-Java approach to Spring MVC configuration and cannot figure out how to associate a MultipartConfigElement
with my DispatcherServlet
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));
Here is the solution compatible with AbstractAnnotationConfigDispatcherServletInitializer
way of configuring the servlet. This is a bit 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 it catching the stack trace of getServletMappings
and thus getting into the code of 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);
}