I am using Jersey client to hit a Spring MVC REST Controller for image uploading functionality. I am getting the following exception:
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.springframework.web.multipart.commons.CommonsMultipartFile, and MIME media type, multipart/form-data, was not found
My Controller Method to POST the Image:
@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
public String fileUpload(@RequestParam("fileUpload") MultipartFile file,
Model model, HttpServletRequest request, HttpServletResponse response)
{
try
{
ClientConfig config = new DefaultClientConfig();
com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client.create(config);
WebResource webResource = client.resource("/save-image");
ClientResponse responseMsg = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.post(ClientResponse.class, file);
}
catch (Exception e)
{
logger.error("Exception in fileUpload()", e);
return "error";
}
return "success";
}
My REST Controller method get the post data:
@ResponseBody
@Consumes(MediaType.MULTIPART_FORM_DATA)
@RequestMapping(value = "/save-image", method = RequestMethod.POST)
public String saveImage(@FormDataParam("file") MultipartFile file, ModelMap
model)
{
//Code to save the image
}
Is there a solution to this exception. I have tried according to the following stack solutions but I am still getting the same exception.
Jersey client exception: A message body writer was not found
Sending multiple files with Jersey: MessageBodyWriter not found for multipart/form-data