Thursday, October 6, 2011

Jersey Client API

Jersey Client API Implementation And Converting JSON String to object

Jersey Client API code receives json data from web service and converts received jason data to the corresponding object ..

Here the code goes .. 


        Client c = Client.create();
        
BASE_URL specifies where my web service resides..
        WebResource res = c.resource(BASE_URI);
        com.sun.jersey.api.client.WebResource.Builder builder = res.accept(MediaType.APPLICATION_JSON_TYPE);
        
This code specifies it ll accept JSON type data and accepts it from web service.

Here the important coding which converts Json type to Object

Declare a generic type object.. Here list of technology is my object , to which i have to convert json string.
In web service i returned  <List<Technology>> as JSON type data

       GenericType<List<Technology>> genericType =  new GenericType<List<Technology>>() {}

       List<Technology> response = builder.get(genericType); 
       
Above code Done it :)

       for (Technology technology : response) {

        System.out.println(technology.getType());

}


Happy Coding
--
Thank you.


Regards,
Kaleeswaran.S

No comments:

Post a Comment