Book Image

Developing RESTful Web Services with Jersey 2.0

By : Sunil Gulabani
Book Image

Developing RESTful Web Services with Jersey 2.0

By: Sunil Gulabani

Overview of this book

<p>JAX-RS 2.0 is an enhanced framework based on RESTful architecture. It provides support for both the client and the server. Jersey 2.0 is the reference implementation of JAX-RS 2.0 (JSR 339 specification). This framework has undergone major revisions. The enhanced framework helps developers to use a low-level and simplified API.</p> <p>This practical, hands-on guide will help you to create RESTful web services easily with individual aspects of the application requests. It will enable you to learn and implement RESTful web services using the new features included in JAX-RS 2.0. It’s a jump start for those who want to try their hand at the new API. It aims to provide practical knowledge of the API along with detailed understanding.</p> <p>This book covers the new features of JAX-RS 2.0. It covers the complete lifecycle of a web service, that is, from server side to client side. The book focuses on the server API and the client API.</p> <p>You will learn how to use the server API in order to create web services that will be deployed on the server. This has all different implementations of HTTP methods and media representations. You will also get acquainted with the client API which specifies how to consume the deployed application’s web services. This includes how to handle different HTTP methods and media representations in response to the web services.</p> <p>You will also get to know Server Sent Events (SSE), which the server uses to push the data event directly to the client. The book will finally take you through the WADL specification. By the end of the book, you will be well equipped to implement Jersey 2.0 and JAX-RS.</p>
Table of Contents (13 chapters)

What is JAX-RS 2.0?


JAX-RS 2.0 is a framework that helps you in writing the RESTful web services on the client side as well as on the server side. Jersey 2.0 is the reference implementation of the JAX-RS 2.0 (JSR 339 specification). Along with the enhancements in Java EE 7, JAX-RS 2.0 has also been revised dramatically.

The following is a list of the RESTful principles that must be followed:

  • An ID should be assigned to everything

  • Things should be linked together

  • A common set of methods must be used

  • Multiple representations should be allowed

  • Stateless communication must be kept

Before moving ahead, let's look at the existing features of JAX-RS 1.0:

  • POJO-based Resource API

  • Provides access to the resource classes using HTTP

  • Format (content types) independence

  • Container (web server) independence

  • Inclusion in Java EE

Features of JAX-RS 2.0

JAX-RS 2.0 remains consistent with the central theme of Java EE 7, but it contains other long-awaited APIs too. These APIs are mostly focused around what is referred to as Simplified API. They can be categorized as follows:

Client API

The specifications in the earlier versions of JAX-RS were only accountable for a server-side API for the RESTful calls. However, the same resulted into the development of different implementers of the server API, that is, Jersey and RESTEasy. This led to the independent development of a client-side API. Now, JAX-RS 2.0 has a Client API that provides support to the server-side specification. This feature is a major incorporation of the previous version of the Jersey Client API. This can be summarized as:

  • Low-level HTTP Client APIs

  • Shared APIs with Server APIs

  • Compatible with some JAX-RS 1.0 implementations

Filters and interceptors

This JAX-RS 2.0 specification is accountable for the client and server filters. In the case of the Client API, implementers provided their own versions of these filters for the client and server. This JAX-RS 2.0 specification, however, absorbs the same into its API. Things that can be done through this specification are:

  • Customization of JAX-RS implementations through well-defined extension points

  • Logging, Compression, Security, and so on can be achieved

  • Shared APIs with Server APIs

  • Compatibility with some JAX-RS 1.0 implementations using some different semantics can be achieved

Client-side and Server-side Asynchronous

This feature allows a request to be dispatched in a nonblocking manner, while the results are made available asynchronously. Also, long-running requests on the server side that are I/O bound can be dispatched. This releases the application server thread and enables it to service other requests. It helps in accomplishing the following tasks:

  • Allows free running of different threads at the server side

  • Request threads can be suspended and resumed as per the need

  • Servlet 3 async support

  • Provides Client API support

Improved Connection Negotiation

This feature helps in automatically determining the response type even if client specification is already present.

Validation

This feature consists of:

  • Services-enabled data validation

  • Bean Validation

  • Inline-constraint annotations on:

    • Fields and properties

    • Request parameters

    • Methods based on response entity

    • Resource classes, that is, path validation using regular expressions

HyperMedia as the Engine of Application State (HATEOAS)

This feature is an important aspect of the RESTful architecture. HATEOAS allows us to provide hyperlinks/URIs in the request/response of the web services. This can be compared with the hyperlinks in an HTML form.

However, Jersey 2.0 can be deployed on several web containers that support Servlet 2.5 or higher, Grizzly 2 HTTP server (which is also the default server for testing), and OSGi containers. For the new async feature of JAX-RS 2.0, Server-Side Events (SSE), we need containers supporting Servlet 3.0.