简体   繁体   中英

Multipart with Spring Boot Rest Service

I have a REST-Service defined with as follow

@RequestMapping(value = "/{userId}/profileimage/{language}", method = RequestMethod.PUT)
public String uploadProfileImage(@PathVariable String userId, @RequestParam MultipartFile file, @PathVariable String language) throws IOException { ...}

and defined a multipart filter

@Bean
public FilterRegistrationBean multipartFilter() {
    FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
    filterRegBean.setFilter(new MultipartFilter());
    List<String> urlPatterns = new ArrayList<String>();
    urlPatterns.add("/*");
    filterRegBean.setUrlPatterns(urlPatterns);
    return filterRegBean;
}

as well as a multipartConfigElement

@Bean
public MultipartConfigElement multipartConfigElement(){
    MultipartConfigElement config = new MultipartConfigElement("");
    return config;
}

But I still get the following Exception when I send a multipart message to the REST service:

Servlet.service() for servlet [dispatcherServlet] in context with path [/dev] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?]

The service is protected by spring security (as a side information) and Spring Boot logs the following on startup

Added existing Filter initializer bean 'multipartFilter'; order=2147483647, resource=class path resource [com/fl/wir/config/MvcConfigurations.class]

Mapping filter: 'multipartFilter' to urls: [/*]

MultipartAutoConfiguration - @ConditionalOnClass classes found: javax.servlet.Servlet,org.springframework.web.multipart.support.StandardServletMultipartResolver,javax.servlet.MultipartConfigElement (OnClassCondition) - matched (OnPropertyCondition)

DispatcherServletAutoConfiguration.DispatcherServletConfiguration#multipartResolver - @ConditionalOnMissingBean (names: multipartResolver; SearchStrategy: all) found the following [multipartResolver] (OnBeanCondition)

Any idea why I still get the exception? (using Spring-Boot 1.2.3.RELEASE)

You are making it way to complex, to enable file uploading simply configure it correctly using properties in the application.properties .

multipart.enabled=true

And make sure you have spring-webmvc on your class path (judging from the annotations used you already have).

However there is one other thing and that is that file upload will only work for POST requests not any other, so PUT will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM