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 works perfectly, but when I have to post mixed and complex params (like MultipartFiles) I get an converter exception.
As an example, I have this:
App1 - PostController:
@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
@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";
}
I get the following error
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)