Rebelek
Rebelek

Reputation: 365

Multipart, spring-boot and JBoss - Required request part 'file' is not present

I have an application that uses spring-boot and embedded Tomcat for development, and on production server it is deployed on JBoss 6.4 EAP. After adding support for JBoss, multipart file upload stopped working. On both containers it was throwing MissingServletRequestPartException.

Adding MultipartConfigElement to ServletRegistrationBean fixed this issue on tomcat, but for JBoss it's still not working. Do you have any ideas where is the problem?

Configuration:

@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet, MultipartConfigElement
        multipartConfig) {
    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
    registration.getUrlMappings().clear();
    registration.addUrlMappings("/*");
    registration.setMultipartConfig(multipartConfig);
    return registration;
}

Endpoint:

@RequestMapping(method = POST)
@ResponseBody
public SomeResponse uploadFileWithComment(
    @RequestParam(value = "file") final MultipartFile file,
    @RequestParam(value = "comment") String comment) {
    ...
}

web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>RestServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.rebelek.Application</param-value>
        </init-param>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>RestServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Upvotes: 3

Views: 2949

Answers (1)

Masud
Masud

Reputation: 555

discussed here https://github.com/spring-projects/spring-boot/issues/2958 suggests if you are using @EnableAutoConfiguration then you need to do the following as because the springboot autoconfig causes issue:

@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})

define the following beans

@Bean(name = "multipartResolver") 
public CommonsMultipartResolver commonsMultipartResolver(){ 
CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(50*1024*1024); return resolver ; 
}


@Bean @Order(0) public MultipartFilter multipartFilter()
{ 
MultipartFilter multipartFilter = new MultipartFilter(); 
multipartFilter.setMultipartResolverBeanName("multipartResolver"); 
return multipartFilter; 
}

Upvotes: 2

Related Questions