Jersey client exception: A message body writer was not found
jersey, rest, web-services
Solution
Register the `MultiPartWriter` provider when creating the `Client`:
ClientConfig cc = new DefaultClientConfig();
Client client;
cc.getClasses().add(MultiPartWriter.class);
client = Client.create(cc);
If using Maven, these are the dependencies you need in your `pom.xml`:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.17.1</version>
</dependency>
Problem
I am using Jersey client to hit a PHP web service for image uploading functionality. I am getting the following exception: ``` Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, multipart/form-data, was not found at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147) ... 63 more ``` This is the code I am using: ``` WebResource webResource = Client.create().resource(HTTP_REST_URI); JSONObject jSONObj = webResource.queryParams(queryParams) .type(MediaType.MULTIPART_FORM_DATA) .post(JSONObject.class, formDataMultiPart); ``` How can this exception be resolved?