Friday, September 27, 2013

JAX-RS 2.0 Client API Generic Type Response Entity

Hi folks!

The last POST we learned how to use the new JAX-RS 2.0 Jersey API, you can read the introduction there. Today we will talk about the detail that was missing for reading JSON responses that uses a generic type.

The code below shows how to setup the WebTarget with the URI you want to request and then it is executed when you call the get() method. Note that this is not different from the first example.
 
Client orderClient = ClientBuilder.newClient();

WebTarget target = orderClient.target("http://localhost:8082/rest-client-api-example/resources/orders");
Response response = target
.request(MediaType.APPLICATION_JSON)
.get();
So, now we will see what really makes the difference using generics. The following line shows how a generic type should be read.

 List<order> orders = response.readEntity(new GenericType<List<Order>>() {});

It doesn't look good, but this anonymous class will do the trick. Remember that you need all the dependencies listed at my github to make this work properly.

This is it, I hope it helped!

9 comments:

  1. Wow! wonderful information, am amazed and i really very impressed very well articles and worth information, i can remember more days that articles.

    SEO Training in Chennai

    ReplyDelete


  2. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


    SEO Company in Chennai

    ReplyDelete
  3. Excellent post ! Thanks for sharing such a useful post.

    digital marketing training

    ReplyDelete
  4. Very Nice Blog I like the way you explained these things. I’ve been looking for ways to improve my website and overall rankings.I hope your future article will help me further.Take SEO Training in Chennai to mould yourself.

    ReplyDelete