Adrian
Adrian

Reputation: 515

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)

Upvotes: 1

Views: 7778

Answers (1)

M. Deinum
M. Deinum

Reputation: 125292

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.

Upvotes: 5

Related Questions