我正在用Spring创建一个REST服务,并收到一个异常,告诉我以下信息。
预期的多部分HttpServlet请求:是否配置了MultipartResolver?
当我将方法= RequestMethod.PUT更改为方法= RequestMethod.POST时,它可以正常工作。
为什么我在PUT上得到这个异常,而在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
4条答案
按热度按时间kg7wmglp1#
Spring使用的多部分支持不支持
POST
以外的其他请求方法,对于StandardServletMultipartResolver
,这是硬编码在该类中的。对于
CommonsMultipartResolver
,它被硬编码在Apache Commons Fileupload项目的ServletFileUpload
实用程序类中。Form-based File Upload in HTML (RFC1867)并没有真正明确地说明这一点,但唯一提到使用的HTTP方法是POST。
简而言之,目前框架只支持POST,您可以通过重新实现一些类来解决它,但它是否能工作(特别是在默认的Servlet 3.0文件上载支持下)可能取决于您的容器。
zdwk9cvp2#
MultipartResolver不支持PUT方法。原因是PUT方法不适用于具有多个参数的表单提交。
相反,我已经尝试了相同的更新方法与PostMapping。因为Multipart不支持Put方法。
q43xntqr3#
我最终使用PUT的原因是使用了
InputStream
而不是MultipartFile
这样,我就可以使用
file body
和multipart body
上载文件内容。guykilcj4#
您可以使用和设置org.springframework.web.multipart.commons.CommonsMultipartResolver来支持POST和PUT方法。示例: