0
votes

I am getting an error by making a post request using jerseyclient. Can anyone take a look at the error below? I would appreciate your feedback.

And here's what I have included as dependencies in pom.xml

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    </dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.19</version>
</dependency>

Error is as below:

com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.lang.String, and MIME media type, application/json, was not found web_1 | at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149) web_1 | at com.sun.jersey.api.client.Client.handle(Client.java:648) web_1 | at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670) web_1 | at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) web_1 | at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563) web_1 | at ....handler.request.WrapperHandler.startWrapper(WrapperHandler.java:65) web_1 | at ....handler.request.WrapperTask.call(WrapperTask.java:35) web_1 | at ....handler.request.WrapperTask.call(WrapperTask.java:8) web_1 | at java.util.concurrent.FutureTask.run(FutureTask.java:262) web_1 | at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152) web_1 | at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:622) web_1 | at java.lang.Thread.run(Thread.java:748) web_1 | Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.lang.String, and MIME media type, application/json, was not found web_1 | at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288) web_1 | at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204) web_1 | at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)

And here's the code which is making a post request.

ClientConfig cc = new DefaultClientConfig();
Client client = Client.create(cc);

WebResource webResource = client.resource(URL.replaceAll(" ", "%20"));
ClientResponse response = webResource
.accept("application/json")
.type("application/json")
.post( ClientResponse.class, parameters.toString() );

String serverOutput = response.getEntity(String.class);
System.out.println("server output=" + serverOutput);
1
Those dependencies you are showing are for Jersey 2.x, but the code you are showing is the 1.x Jersey client. They are not compatible For 1.x JSON support, you should use the jersey-json dependency. 1.x and 2.x are not compatible. - Paul Samsotha
@PaulSamsotha I have jersey-json dependency defined. However, the version is 1.19. Are you suggesting that I should update the version of it? <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.19</version> </dependency> - ejshin1

1 Answers

0
votes

You need to add the json parser to the default client

apiClient is the code that understand the json objects.

JacksonJsonProvider jsonProvider = new JacksonJsonProvider(apiClient.getObjectMapper()); DefaultClientConfig defaultClientConfig = new DefaultClientConfig(); defaultClientConfig.getSingletons().add(jsonProvider);