Book Image

Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON

By : Bhakti Mehta, Masoud Kalali
Book Image

Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON

By: Bhakti Mehta, Masoud Kalali

Overview of this book

<p>As the technology landscape moves focus towards smaller devices, the need for building scalable, flexible, lightweight, and real-time communications-based applications grows. HTML 5 and Java EE 7 provide a new synthesis of technologies that demonstrate tremendous scope and potential in areas of device independence, asynchronous communication, interoperability, and portability.<br /><br />Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON is a practical, hands-on guide that provides you with clear and pragmatic information to take advantage of the real power behind HTML5 and Java EE technologies. This book also gives you a good foundation for using them in your applications.<br /><br />Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON looks at the different HTML5-based Java EE 7 API, and takes a deep dive into the individual areas of technologies to cover basic to advanced concepts, and also provides best practices for each API. You will also learn how to build a REST-based Event Notification Application using the Twitter API, tying all the different technologies together that we will cover. You will also take a look at integrating different Java EE APIs to build a Library Application. If you want to take advantage of using the new HTML5 technologies and Java EE 7 platform, then this is the book for you. You will learn everything you need to know to build portable RESTful Web Services with JAX-RS 2.0, Web Sockets, JSON, and Server-Sent Events.</p>
Table of Contents (12 chapters)

Preface

Over the years, we have seen several revolutions, and shifts in paradigms spanning from mainframes to x86 farms, from heavyweight methodologies to lightweight agile methods, and from desktop and thick clients to thin, rich, and highly available web applications and ubiquitous computing.

With the advancements and changes in the technology landscape going towards smaller, more portable, and lightweight devices and the devices being used widely for day-to-day activities, the need to push the computation from the client machines to the backend grows to an even more prominent one. This also brings forth opportunities and challenges involved in developing applications with near real-time or real-time event and data propagation between servers and clients; that is where HTML 5 provides developers with the standards, API, and flexibility required to achieve the same result in web applications that is achievable in thick desktop applications.

The communication between clients and the servers has turned to the most fundamental subject both in terms of quantity, content, interoperability, and scalability of these interactions. The XML era, long waiting requests, single browser, and single device compatibility is over; instead the era of devices, multiple clients, and browsers, from very small devices capable of only processing text over HTTP to mammoth scale machines processing almost any kind of content has begun. With this said, producing the content and accepting the content along with the ability to switch between older and newer protocols has turned into an obvious must.

Java EE 7 comes with more emphasis on these emerging (and dominating) requirements; support for HTML5, more asynchronous communication/invocation-capable components, and support for JSON as one of the data formats have arrived to help developers with resolving the technical requirements and giving the developers ample time to work on the business requirements' implementation of their systems.

This book is an attempt to provide the avid technologists with an overview of what Java EE is in general and Java EE 7 in particular, as a technology platform, provides for developing lightweight, interactive applications based on HTML5 deployable in any Java EE compatible container.

What this book covers

Chapter 1, Building RESTful Web Services Using JAX-RS, starts with the basic concepts of building RESTful Web Services and covers JAX-RS 2.0 API, detailing the different annotations, Providers, MessageBodyReader, MessageBodyWriter, Client API, and Bean Validation support in JAX-RS 2.0.

Chapter 2, WebSockets and Server-sent Events, discusses the different programming models for sending near real-time updates to clients. It also covers WebSockets and Server-sent Events, the JavaScript and Java API for WebSockets and Server-sent Events. This chapter compares and contrasts WebSockets and Server-sent Events and shows the advantages of WebSockets to reduce unnecessary network traffic and improve the performance.

Chapter 3, Understanding WebSockets and Server-sent Events in Detail, covers the Java EE 7 API for WebSockets, Encoders and Decoders, the Client API, how to send different kinds of messages with WebSockets using blobs, and ArrayBuffers. It teaches how to secure a WebSockets-based application. It outlines the best practices for WebSockets and Server-sent Events-based applications.

Chapter 4, JSON and Asynchronous Processing, covers the Java EE 7 JSON-P API for parsing and manipulating JSON data. It also discusses the new NIO API introduced in Servlet 3.1 specification. It teaches how to use the JAX-RS 2.0 API for asynchronous request processing to improve scalability.

Chapter 5, RESTful Web Services by Example, covers two real-life examples of RESTful Web Services. It covers an event notification sample based on the Twitter Search API, how the server can push the data to clients as and when events occur. A library application ties the different technologies covered in the above chapters together.

What you need for this book

To be able to build and run samples provided with this book you will need:

  1. Apache Maven 3.0 and higher. Maven is used to build the samples. You can download Apache Maven from http://maven.apache.org/download.cgi

  2. GlassFish Server Open Source Edition v4.0 is the free, community-supported Application Server providing implementation for Java EE 7 specifications. You can download the GlassFish Server from http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/

Who this book is for

This book is a perfect reading source for application developers who are familiar with Java EE and are keen to understand the new HTML5-related functionality introduced in Java EE 7 to improve productivity. To take full advantage of this book, you need to be familiar with Java EE and have some basic understanding of using GlassFish application server.

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 words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The request, which is sent to the JAX-RS resource, is a POST request with app/library/book/ as the target URI."

A block of code is set as follows:

@GET
@Path("browse")
public List<Book> browseCollection() {
  return bookService.getBooks();    }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

@GET
@Path("browse")
public List<Book> browseCollection() {
  return bookService.getBooks();    }

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: "When a user clicks on the Hold button on the HTML page".

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 , 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 if you are having a problem with any aspect of the book, and we will do our best to address it.