Monday, January 27, 2014

RESTful Web Services With JSON response

With Jersey + Jackson + Jersey Java Client


Jersey uses Jackson to convert object to / form JSON. In this tutorial, we show you how to convert a “TTInformation” object into JSON format, and return it back to user.

1) Dependencies

To make Jersey support JSON mapping, use “jersey-json_version.jar” 
To use Jersey client APIs, declares “jersey-client_version.jar

Please look at selected ones.


2) Integrate JSON with Jersey

In web.xml, declares “com.sun.jersey.api.json.POJOMappingFeature” as “init-param” in Jersey mapped servlet. It will make Jersey support JSON/object mapping.

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature
<param-value>true
</init-param>

File : web.xml - full example.



3) POJO

A simple “TTInformation” object, later Jersey will convert it into JSON format.




4) JAX-RS with Jersey - GET and POST service

Annotate the method with @Produces(MediaType.APPLICATION_JSON). Jersey will use Jackson to handle the JSON conversion automatically.


5) Want to see OutPut     .... :)

6) Standalone Jersey GET Client
Jersey client to send a “GET” request and print out the returned json data.

Output

7) Standalone Jersey POST Client
Jersey client to send a “POST” request, with json data and print out the returned output.

Output

For source code -> sac10nikam@gmail.com

No comments: