Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultipartFile is null when I use CommonsMultipartResolver in my Spring Boot app

I would like to implement upload handler method in my Spring Boot app. I want to use CommonsMultipartResolver instead of StandardServletMultipartResolver. I have the the following problem:

My controller method parameter (file) is always set to null. I saw in java docs of HiddenHttpMethodFilter: This filter needs to run after multipart processing in case of a multipart POST request, due to its inherent need for checking a POST body parameter.

I tried to set order for the multipartFilter bean but it did not help. When I debug, I see that HiddenHttpMethodFilter is always called before MultipartFilter. I am using Spring Boot version 1.3.0.RELEASE and Java based config. How can I set the order that HiddenHttpMethodFilter comes after MultipartFilter?

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();        
    resolver.setMaxUploadSizePerFile(52428800); 
    return resolver;
}

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

@RequestMapping(value = "/{userId}", method = RequestMethod.POST)
@ResponseBody
public FileTransport create(@PathVariable("userId") Long userId,
        MultipartFile file) throws IOException {
    //return fileService.create(userId, file);
    return new FileTransport();
}

If I modify the method to include Request param like below:

@RequestParam(name = "file", required = true) MultipartFile file

It throws an exception: 'Required MultipartFile parameter 'file' is not present'.

If I use StandardServletMultipartResolver, everything works as expected.

like image 661
f.u. Avatar asked Dec 18 '22 16:12

f.u.


2 Answers

In Spring Boot to use CommonsMultipartResolver you need to disable MultipartAutoConfiguration by adding following annotation to your configuration:

@EnableAutoConfiguration(exclude={MultipartAutoConfiguration.class})

My multipart resolver bean:

 @Bean
 public MultipartResolver multipartResolver() {
     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
     multipartResolver.setMaxUploadSize(5 * 1024 * 1024);
     return multipartResolver;
 }
like image 174
Alexander Avatar answered May 09 '23 07:05

Alexander


I think they have the resolved the issue in Spring-Boot 1.4.2.RELEASE version:

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipart = new CommonsMultipartResolver();
    multipart.setMaxUploadSize(3 * 1024 * 1024);
    return multipart;
}

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

You also need to exclude Spring-Boot's MultipartAutoConfiguration.class

@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})

This worked for me.

like image 20
Jay Modi Avatar answered May 09 '23 06:05

Jay Modi