Note that there are some explanatory texts on larger screens.

plurals
  1. POSending Multipart File as POST parameters with RestTemplate requests
    primarykey
    data
    text
    <p>I am working with Spring 3 and RestTemplate. I have basically, two applications and one of them have to post values to the other app. through rest template. </p> <p>When the values to post are Strings, it's work perfect, but when i have to post mixed and complex params (like MultipartFiles) i get an converter exception.</p> <p>As example, i have this:</p> <p>App1 - <strong>PostController:</strong></p> <pre><code>@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute UploadDTO pUploadDTO, BindingResult pResult) throws URISyntaxException, IOException { URI uri = new URI("http://localhost:8080/app2/file/receiver"); MultiValueMap&lt;String, Object&gt; mvm = new LinkedMultiValueMap&lt;String, Object&gt;(); mvm.add("param1", "TestParameter"); mvm.add("file", pUploadDTO.getFile()); // MultipartFile Map result = restTemplate.postForObject(uri, mvm, Map.class); return "redirect:postupload"; } </code></pre> <p>On the other side... i have another web application (App2) that receives the parameters from the App1.</p> <p>App2 - <strong>ReceiverController</strong></p> <pre><code>@RequestMapping(value = "/receiver", method = { RequestMethod.POST }) public String processUploadFile( @RequestParam(value = "param1") String param1, @RequestParam(value = "file") MultipartFile file) { if (file == null) { System.out.println("Shit!... is null"); } else { System.out.println("Yes!... work done!"); } return "redirect:postupload"; } </code></pre> <p>My application-context.xml:</p> <pre><code>&lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /&gt; &lt;bean class="org.springframework.http.converter.FormHttpMessageConverter" /&gt; &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter" /&gt; &lt;bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt; &lt;property name="maxUploadSize"&gt; &lt;value&gt;104857600&lt;/value&gt; &lt;/property&gt; &lt;property name="maxInMemorySize"&gt; &lt;value&gt;4096&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>Here is the stack of the exception that i am getting when i do the postForObject of the RestTemplate...</p> <pre><code>org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.commons.CommonsMultipartFile] at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:292) at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:252) at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:242) at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:194) at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:1) at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:588) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:436) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415) at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:294) at com.yoostar.admintool.web.UploadTestController.create(UploadTestController.java:86) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619) </code></pre> <p>So my questions are:</p> <ol> <li>Is it possible to send MultipartFile through RestTemplate using POST?</li> <li>Are there some specific converters that I have to use to send this type of objects? I mean is there some <strong><em>MultipartFileHttpMessageConverter</em></strong> to use in my configuration?</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload