2014-09-02 18 views
6

Sto creando un servizio REST con Spring e ottengo un'eccezione che mi dice quanto segue.Spring Boot MultipartResolver mancante nel metodo PUT

Previsto MultipartHttpServletRequest: è un MultipartResolver configurato?

Quando cambio metodo = RequestMethod.PUT in method = RequestMethod.POST funziona.

Perché ottengo questa eccezione su PUT ma non sul 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

Non sono sicuro che PUT sia incluso nelle specifiche per richieste multipart (nelle specifiche servlet). Come stai inviando i dati? –

risposta

10

Il supporto più parti come utilizzata Spring non supporta l'altro metodo di richiesta poi POST. Per lo StandardServletMultipartResolver questo è codificato in quella classe.

Per il CommonsMultipartResolver è codificato nella classe di utilità ServletFileUpload dal progetto Apache Commons Fileupload.

Tthe Form-based File Upload in HTML (RFC1867) non è veramente esplicito su questo, ma l'unica menzione di un metodo HTTP utilizzato è POST.

In breve, al momento solo POST è supportato dai framework che potresti riuscire a risolvere aggirando alcune classi, ma se funziona (specialmente con il supporto di caricamento file Servlet 3.0 predefinito) potrebbe dipendere dal tuo contenitore.

0

Ciò che funziona per me con PUT è utilizzando un InputStream invece di MultipartFile

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

In questo modo sono stato in grado di utilizzare file body e multipart body per caricare il contenuto del file.