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