Jersey 2.x web service using JAXB + JSON example

In the previous article, we learnt and implemented an example on how to use JAX-RS Restful web service to send & receive XML data as request/response. Here, we will re-use and modify the same example to send & receive JSON data as web service request/response

Jersey is the most popular amongst Restful web service development. Latest Jersey 2.x version has been developed by Oracle/Glassfish team in accordance with JAX-RS 2.0 specification. Earlier Jersey 1.x version was developed and supported by Oracle/Sun team

Latest Jersey release version is 2.12 see here and look documentation and API for details. We will implement Jersey examples in the following articles based on latest 2.x version

Still we need Java objects to send/receive data in JSON format so start designing your XML Schema Definition (XSD), as we can use JAXB Maven plugin to generate Java classes. Later this generated POJO used to exchange JSON data on the fly with the help of Jackson library

Note: Jackson – high performance parser is included as dependencies for JSON support

Folks who aren’t familiar with Maven concepts or don’t require maven for their project, can download the below jars individually from the central repository or maven repository and include them in the classpath

Project Structure (Package Explorer view in Eclipse)

Jars Libraries Used in the Project (Maven Dependencies)

Web application

For any web application, entry point is web.xml which describes how the incoming http requests are served / processed. Further, it describes about the global-context and local-context param (i.e.; <context-param> & <init-param>) for loading files particular to project requirements & contains respective listener

With this introduction, we will understand how we configured web.xml for Jersey JAX-RS Restful web service

web.xml (the entry point –> under WEB-INF)

This web.xml file describes,

Like any JEE web framework register “org.glassfish.jersey.servlet.ServletContainer” with servlet container

http requests with URL pattern “/rest/*” will be sent to the registered servlet called “jersey-servlet” i.e.; (org.glassfish.jersey.servlet.ServletContainer)

Set <init-param> with <param-name> as “jersey.config.server.provider.packages” and <param-value> describing the qualified package name of the JAX-RS annotated Resource/Provider classes. In this example, “com.jersey.series.json.service”

Let’s see coding in action

URL Pattern

Http url for any common web application is http://<server>:<port>/<root-context>/<from_here_application_specific_path>

In our example, we are going to deploy the war into Tomcat 8.0 server so our server and port are localhost and 8080 respectively. Context root is the project name i.e.; Jersey-JSON-IO. Initial path for this application is http://localhost:8080/Jersey-JSON-IO

We have configured “/rest/*” as url-pattern for the “jersey-servlet” servlet in web.xml and at class-level path configured is “/bookservice” using @Path annotation. Next respective path for each method annotated with @Path (method-level)

Book Service interface

Defines basic CRUD operations for Book Service

NOTE: It’s always a good programming practice to do code-to-interface and have its implementation separately

Conclusion: Thus, JSON format can be used to send/receive data where performance is a big consideration as it is light-weight. Especially, in mobile development client prefer to use JSON data for web service interaction