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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With