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)

Verbs in REST


Some of the requests used in REST are as follows:

  • GET: The GET request retrieves a representation of a resource from server to client

  • POST: The POST request is used to create a resource on the server based on the representation that the client sends

  • PUT: The PUT request is used to update or create a reference to a resource on server

  • DELETE: The DELETE request can delete a resource on server

  • HEAD: The HEAD requests checks for a resource without retrieving it

The next section will introduce the notion of safety and idempotence, two important terms associated with REST.

Safety and idempotence

When it comes to REST, a safe method, by definition, is a HTTP method that does not modify the state of the resource on the server. For example, invoking a GET or a HEAD method on the resource URL should never change the resource on the server. PUT is considered not safe since it usually creates a resource on the server. DELETE is also considered not safe since it will delete the resource on the server. POST is not safe since it will change the resource on the server.

Idempotent method is a method that can be called multiple times yet the outcome will not change.

GET and HEAD are idempotent, which means that even though the same operation is done multiple times the result does not vary. PUT is idempotent; calling the PUT method multiple times will not change the result and the resource state is exactly the same.

DELETE is idempotent because once the resource is deleted it is gone, and calling the same operation multiple times will not change the outcome.

In contrast, POST is not idempotent and calling POST multiple times can have different outcomes.

Tip

The idempotence and safety of the HTTP verbs are a convention, meaning that when someone is using your API they will assume that GET/PUT/POST/DELETE have the same idempotency characteristics that are previously described; and the implementation of the business logic behind each verb should support these characteristics.

The response sent by the server could be in XML, JSON, or any other MIME type as long as the server supports the requested format. In case the server cannot support the requested MIME type, it can return with a status code of 406 (not acceptable).

When we are developing with RESTful principles in mind, each message should have enough information to let the server understand the purpose of the message and how to process that message, to produce the response the message is meant for, and finally to ensure visibility and statelessness.

Summarizing, these are the components of RESTful Web Services:

  • Base URI: The base URI for the Web Service http://foo.com/bar

  • Media type: The media type supported by the Web Service

  • Methods: The HTTP methods such as GET, PUT, POST, and DELETE