2014-09-02 82 views
6

我正在用Spring創建一個REST服務,並得到一個異常告訴我以下內容。PUT方法缺少Spring Boot MultipartResolver

預期MultipartHttpServletRequest:是MultipartResolver 配置?

當我將method = RequestMethod.PUT更改爲method = 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 
+1

我不確定PUT是否包含在多部分請求規範中(在servlet規範中)。你如何發送數據? – 2014-09-03 06:07:50

回答

10

Spring所使用的multipart支持不支持其他請求方法,然後POST。對於StandardServletMultipartResolver,這是在該類中硬編碼的。

對於CommonsMultipartResolver,它在ServletFileUpload實用程序類中從Apache Commons Fileupload項目進行了硬編碼。

Form-based File Upload in HTML (RFC1867)對此並不十分明確,但唯一提及的使用HTTP方法是POST。

簡而言之,目前只有POST被框架支持,您可以通過重新實現一些類來解決它,但是如果它能夠工作(特別是默認的Servlet 3.0文件上傳支持)可能取決於您的容器。

0

什麼用PUT對我的作品是用的,而不是InputStreamMultipartFile

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

這樣我就能夠使用file bodymultipart body上傳的文件內容。

相關問題