Spring Boot MultipartResolver missing in PUT method

I am creating a REST service using Spring and I get an exception telling me the following.

HttpServletRequest expected: is a multithreaded resolver configured?

When I change the method = RequestMethod.PUT to the method = RequestMethod.POST, it works.

Why am I getting this exception in PUT, but not in POST?

@Configuration @ComponentScan("io.myservice") @EnableAutoConfiguration @EnableCaching @EnableAsync(mode = ASPECTJ) public class Application implements AsyncConfigurer { static org.slf4j.Logger LOG = LoggerFactory.getLogger(Application.class); public static final String MAX_FILE_SIZE = "2MB"; public static final String MAX_REQUEST_SIZE = "2MB"; public static final String FILE_SIZE_THRESHOLD = "2MB"; @Value("${app.dir.incoming}") public String createdDir; @Bean public LocalValidatorFactoryBean localValidatorFactoryBean() { return new LocalValidatorFactoryBean(); } @Bean MultipartConfigElement multipartConfigElement() { String absTempPath = new File(createdDir).getAbsolutePath(); MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setMaxFileSize(MAX_FILE_SIZE); factory.setMaxRequestSize(MAX_REQUEST_SIZE); factory.setFileSizeThreshold(FILE_SIZE_THRESHOLD); factory.setLocation(absTempPath); return factory.createMultipartConfig(); } @Bean public StandardServletMultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } @Override @Bean public ThreadPoolTaskExecutor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(2); executor.setQueueCapacity(5); executor.initialize(); return executor; } @Bean public SimpleCacheManager cacheManager(){ SimpleCacheManager cacheManager = new SimpleCacheManager(); List<Cache> caches = new ArrayList<Cache>(); caches.add(cacheBean()); cacheManager.setCaches(caches ); return cacheManager; } @Bean public Cache cacheBean(){ Cache cache = new ConcurrentMapCache("default"); return cache; } public static void main(String[] args) throws IOException { SysOutOverSLF4J.sendSystemOutAndErrToSLF4J(); run(Application.class, args); } } @RequestMapping(value="/converter", method=RequestMethod.PUT) @ResponseBody public String convert(MultipartFile file) { LOG.info("received new file to convert") } Caused by: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured? at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:171) at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:89) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) ... 37 common frames omitted 
+7
source share
3 answers

The multicast support used by Spring does not support another request method, and then POST . For StandardServletMultipartResolver it is hardcoded in this class.

For CommonsMultipartResolver it is hardcoded in the utility class ServletFileUpload from Apache Commons Fileupload .

Tthe HTML form-based file upload (RFC1867) is not really explicit, but the only mention of the HTTP method used is POST.

In short, at the moment, only POST is supported by infrastructures that you can work around by reworking some classes, but if it works (especially with the support for loading files by default, Servlet 3.0), it may depend on your container.

+13
source

What ended up working for me with PUT was using InputStream instead of MultipartFile

  @RequestMapping(value="/converter", method=RequestMethod.PUT) @ResponseBody public String convert(InputStream file) 

That way I could use file body and multipart body to load the contents of the file.

0
source

MultipartResolver does not support the PUT method. Because the PUT method is not suitable for submitting a form with multiple arguments.

Rather, I tried with PostMapping for the same update method. Since Multipart does not support the Put method.

0
source

Source: https://habr.com/ru/post/974718/


All Articles