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)

Preface

This book is for developing the JAX-RS 2.0 RESTful web services using Java. It provides an understanding of new features of JAX-RS 2.0, along with practical examples for the server and client side. It also covers implementation of different media representations, such as JSON, XML, and multipart. Apart from this, we have also included the modern HTML5-feature of Server-Sent Events (SSE). SSE is basically used for real-time applications, where a server pushes events to the client. Lastly, we described how to generate an XML specification named Web Application Description Language (WADL) of the web services.

What this book covers

Chapter 1, Getting Started, gives a brief note regarding the new features that have been introduced in JAX-RS 2.0, such as client API, filters and interceptors, and client-side and server-side asynchronous. It also contains some other prominent features, such as listing of modules and dependencies that are used for implementing JAX-RS 2.0.

Chapter 2, Server API, covers how to create resource classes and methods, usage of parameter annotations to access user-defined values, and subresources to consume specific resource methods. We will see when the root-resource classes are accessible, and the rules for injecting the path annotations to access values. Lastly, we will learn how to load the resource classes using different methods of the Application model, ResourceConfig, and Without Application Model.

Chapter3, Client API, shows how to consume the RESTful web services that are using the JAX-RS client API. We will cover how to call web services for different HTTP headers and *Param annotations, so that we can then perform all the CRUD operations using this client API. JAX-RS is a wrapper class of the HTTP, so we can use any web services that are based on the HTTP protocol. JAX-RS client API follows the uniformity that implements the REST architecture.

Chapter 4, Common Media-Type Representations, covers different representations of the data. Data representation is the primary decision for any application. We need to decide an appropriate representation on the basis of the client that will consume the web services. We will also go through the implementation of different representations on the server side as well as the client side.

Chapter 5, Server-Sent Events (SSE), covers how to create a connection between the client/server and maintain the connection at the server's end. This is needed to push the data from the server to the client without any new request initiated by the client. This type of mechanism is basically used for applications such as chatting, stock market, or any real-time data-providing applications.

Chapter 6, WADL, describes Web Application Description Language (WADL), which is a skeleton of the deployed RESTful web service.

What you need for this book

  • Understanding of RESTful web services

  • Java EE

  • Eclipse IDE

  • JAX-RS 1.0 knowledge (optional)

Who this book is for

This book is intended for the Java EE developers who are building the application on the REST architecture. This is a quick handbook for learning JAX-RS 2.0. Developers should be having knowledge about the RESTful web services and not mandated to JAX-RS 1.0.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code and the output being generated by the code are added to the book as screenshots.

A block of code is set as follows:

@Path("/getResource")
public class GetResource {
    @GET
    public String get() {
        return "Hello World!!!";
    }

    @GET
    @Path("{name}")
    public Response greetUser(@PathParam("name") String name){
        returnResponse.status(200).entity("Hello, " + name).build();
    }
}

New terms and important words are shown in bold. Words that you see on the screen, in menus, or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to or , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at or if you are having a problem with any aspect of the book, and we will do our best to address it.