Sunday, May 13, 2012

Jersey Implementation

 Jersey Implemmentaion:
  • Open source, dual-licensed, production-quality JAX-RS reference implementation for building RESTful Web services. 
  • It's meant to be more than just a reference implementation and provides APIs that allow easy customization and extension by developers. 
  •  
  • Jersey ships as part of Sun’s GlassFish application server download.

Embedded containers:

Jersey is usually deployed within a servlet container but does support an embedded mode of operation within Java programs. Running a JAX-RS service in an embedded mode is easy and simple and requires only a few lines of code. You can also use this embeddable container easily with unit tests.

Client API:

The Jersey client API is a sophisticated, high-level, Java-based API for invoking any RESTful Web service, not just JAX-RS-compliant services. JAX-RS developers however, should find the Jersey client API easy and familiar to use. The Jersey client API claims to have three important goals:

    Encapsulate the REST constraint of Uniform Interface Constraint on the client side.
    Make it as easy to interoperate with server-side RESTful Web services.
    Leverage JAX-RS API concepts and artifacts for the client side.

The Jersey client API also allows a pluggable HTTP implementation (like HttpURLConnection and the Apache HTTP client). Overall, the Jersey client API lets you efficiently implement a REST-based client-side solution.

Below example of Jersey client code that enables sending a POST request with form parameters and receiving a response as a JAXB object.
 
Jersey client code:

 Form form = new Form();
f.add(“a”, “dim”);
f.add(“b”, “sum”);
Client client = Client.create();
WebResource resource =   client.resource(“http://localhost:8080/formpost”);
JAXBBean bean = resource.
    type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).
    accept(MediaType.APPLICATION_JSON_TYPE).
    post(JAXBBean.class, form);


It is important to note that the same code, if written using HttpURLConnection, would have involved a lot more work trying to serialize the form variables and de-serialize the response to the JAXB bean.

Interceptor framework:

Jersey provides a filter-based Interceptor framework that allows registering two types of filters:

    Container filters. Container filters filter the requests before resource filters.
    Resource filters. Resource filters filter responses before container filters.

Data format support:

Jersey, like other JAX-RS implementations, also provides JAX-RS extension modules for supporting common formats, including Atom, JSON, and MIME multipart data. Atom support requires a dependency on Apache Abdera as well as the jersey-atom-abdera module.

Component integration:

Jersey currently provides extension-based support for two dependency injection frameworks: the Spring Framework and the Google Guice framework:

    Spring Framework. Spring support in Jersey requires a dependency on the jersey-spring module. Spring support is enabled by referencing the SpringServlet class in the web.xml file.
    Google Guice framework. Guice support is provided when referencing the Guice filter GuiceFilter and a Guice-specific ServletContextListener in the web.xml file.

No comments:

Post a Comment