JAX-RS (REST)1 Comment

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

  • Dependency

To make Jersey support JSON mapping, declares “jersey-json.jar” in Maven pom.xml file.

Review the downloaded dependencies in your project classpath, Jackson and related libraries are inlcuded.
  • 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.

File : web.xml – full example.

3. Simple Object

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

  • JAX-RS with Jersey

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

  • Demo

See demo for GET and POST request.

1. GET method
When URI pattern “/json/metallica/get” is requested, the Metallica classic song “Enter Sandman” will be returned in JSON format.

2. POST method
To test post request, you can create a RESTful client (refer to this Jersey client APIs example), and “post” the json format string to URI pattern “/json/metallica/post“, the posted json string will be converted into “Track” object automatically.


1 Comment on this article

Add a comment


Warning: stream_socket_client(): unable to connect to http://browscap.org/version (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 1366

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 876