Announcement Announcement Module
Collapse
No announcement yet.
Post Multipartfile to Rest Service using rest template Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Post Multipartfile to Rest Service using rest template

    Hi guys,

    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.

    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.

    As example, i have this:

    App1 - PostController:


    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<String, Object> mvm = new LinkedMultiValueMap<String, Object>();
    		mvm.add("param1", "TestParameter");
    		mvm.add("file", pUploadDTO.getFile()); // MultipartFile
    				
    		Map result = restTemplate.postForObject(uri, mvm, Map.class);
    		return "redirect:postupload";
    	}
    On the other side... i have another web application (App2) that receives the parameters from the App1.

    App2 - ReceiverController

    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";
    	}
    My application-context.xml:

    HTML Code:
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    		<property name="messageConverters">
    			<list>
    				<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
    			</list>
    		</property>
    	</bean>
    			
    	<bean id="multipartResolver"  
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
            <property name="maxUploadSize">  
                <value>104857600</value>  
            </property>  
            <property name="maxInMemorySize">  
                <value>4096</value>  
            </property>      
    	</bean>  
    Here is the stack of the exception that i am getting when i do the postForObject of the RestTemplate...

    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)
    So my questions are:
    1. It's possible to send MultipartFile through RestTemplate using POST?
    1. There are some specific converter that i have to use to send this type of objects? I mean there is some MultipartFileHttpMessageConverter to use in my configuration?

    Thanks in advance..
    Mauro.

  • #2
    Hi,

    There doesn't seem to be a converter for multipart content that ships with Spring. I suppose you could implement one yourself, or manually create the content as String. If the server requires you to set specific request headers, you should use RestTemplate.exchange() method.
    Last edited by guznik; Nov 8th, 2010, 03:54 AM.

    Comment

    Working...
    X