I am need to call the a resful api that will return json response. I am thinking about using jersey client api but not sure if it is in any way better than just using HttpClient directly and then using GSON to convert the response to Java objects.
Jersey client is far better than HttpClient from a coding efficiency standpoint. Consider:
// Jersey client
WebResource resource = Client.create().resource("http://foo.com")
resource.path("widgets").entity(someWidget).type(APPLICATION_JSON).post();
Wodget wodget = resource.path("widgets/wodget").accept(APPLICATION_XML).get(Wodget.class);
as opposed to:
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://foo.com/widgets");
HttpEntity someWidgetEntity = ... // something with GSON to marshal the 'someWidget'
httpPost.setEntity(someWidgetEntity);
HttpResponse response = httpclient.execute(httpPost);
HttpGet httpGet = new HttpGet("http://foo.com/widgets/wodget");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
... // something with GSON to read in the wodget
}
Add in the fact that the Jersey client can use HttpClient under the covers for its HTTP interaction, and you get the best of both worlds: a simplified interface plus the power of a widely-used and versatile HTTP client library.
Note: Above code is completely untested but roughly accurate in shape.
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