Tag Archives: Rest

Asynchronous Rest Services With Jersey

Asynchronous Rest Services With Jersey


There are two major versions of Jersey : Jersey 1 (com.sun.jersey.*) and Jersey 2 (org.glassfish.jersey.*). Currently Jersey 2 is being used but internet has information on both of them and lot has changed between the two.

Jersey can be used with a servlet container such as Jetty Servlet Container, Grizzly Servlet Container and any Generic Servlet 3.x and 2.x containers (tomcat) OR HTTP container such as Grizzly, JDK HTTP Container, Simple HTTP Container, Jetty HTTP Container

2015-03-28 20_33_33-Clipboard


In Jersey, you can return domain objects such as Book or Collection of Books (see below image). This works fine when you don’t want to specify any response details such as HTTP headers or status codes.


You can write tests by extending the JerseyTest testing framework, In the below image, a test to get a book by passing path id and getting Collection of Books is used. Note that you cannot directly pass Collection<Book> because the de-serialization from JSON to Java Object/Data structure will not work. To make this work Jersey provides a GenericType which can be written as (new GenericType<Collection<Book>>(){});


Example of a POST Request: A post request requires entity to be instantiated.




Jackson is a suite of data processing tools for java that support JSON, XML and other formats. Jersey by default comes with Moxy and we can switch to jackson by removing the dependency for moxy  (org.glassfish.jersey.media) and adding the Jackson Provider (Group ID: com.fasterxml.jackson.jaxrs).  Artifact Id can be jackson-jaxrs-json-provider and jackson-jaxrs-xml-provider. You can configure the Jackson Provider:

JacksonJsonProvider json = new JacksonJsonProvider();



Jackson Annotations




Tools for Rest API

Tools For Rest API


A good documentation and healthy rest API request response makes the application more robust, easy to develop, test and maintain. There are three services I came across, that not only document but gives you a testing sandbox from your services and will help your applications grow with managed design and architecture.


Rest Clients

There are many rest clients for chrome and Firefox but the one I liked most is postman as a packaged application. Some of it’s salient features are:

  • it allows you to create cURL scripts out of your requests.
  • it allows you to manage environments by introducing variables such as {{baseURL}} which can be different for Production, QA and development.
  • You can save the responses in the paid version.
  • Has a proxy mode and authentication.


Although rest clients allow you to send the requests and receive the response, HTTP Debuggers allow you to see what happens under the hood and act on the data. Example if a server responds you with JSON, you can intercept it and convert it into XML before the responds reaches the client. You need to configure For chrome to send the requests through this proxy. Some debuggers are:

  • Burp – Good for intercepting requests and responses and for testing security like adding JavaScript in the response.
  • Fiddler
  • Charles

Packet Sniffers

To see what is going on the network interfaces at a much lower level. No need to setup proxy and you can filter by http. You will see data in TCP packets so it will be a breakdown of data. To see the actual request and response, you can right click and click on “Follow TCP Streams” to see the combination of data presented as one request.

  • Wireshark



The client (browser) can cache data based on the HTTP Expires tag. The server can set the Expires tag (HTTP 1.0) and Cache-Control (HTTP 1.1) with HTTP Response. It applies to HTML files, images and resources. Using cache-control you can set the time the resource should be cached (max-age — local client), you can also set the cache-control for proxy (s-maxage). The advantage is less network calls and the disadvantage is stale data. In the case where there is authorization in headers or https, browser doesn’t use caching even if the headers are present.

Validation Caching

To cache personalized resources where expiration caching doesn’t help, validation caching is used. The response has an E-Tag with a certain value. Example, e-tag:4x4hij. Until this e-tag doesn’t change all the client requests coming for the resource are sent back with 304 (Not Modified). The same works with Last-Modified and If-Modified-Since.

 Performance Testing

There are good cloud based load testing tools, that can do stress test to the API.


  •  Splunk
  • Loggly