The REST Way!

Building Web Services in Java

REST – REpresentational State Transfer is an architectural style for accessing information on the web. This is becoming increasingly popular and an easier way to access services on the web. In the REST architecture style, information that is available on the server side is considered as resources and these resources can be accessed in a uniform way using web – Uniform Resource Identifier (URI) and the web protocol, HTTP. Since HTTP is preferred as the protocol for communication, REST constraints the architecture to client-server architecture which is stateless in nature. Resources on the web are identified by URIs. Web Services that are created and accessed using REST principles are called RESTful Web Services.

RESTful Web Services use the methods of HTTP protocol for the operations they perform. HTTP methods POST, GET, PUT and DELETE can be mapped to operations like Create, Read, Update and Delete (CRUD). RESTful Web Services in Java can be built using JAX-RS – Java API for RESTful Web Services which is introduced as part of Java EE 6 Platform and it fully supports REST principles.

JAX-RS – An Introduction

JAX-RS (JSR311) is a Java programming API that is included as part of the new Java EE 6 Platform. JAX-RS is designed to make it easy for developing applications in Java using REST principles / architecture. JAX-RS follows the goals of Java EE – simplicity and ease of development. JAX-RS API uses annotations which makes development of web services using REST architecture simple and easy. Simple POJOs can be exposed as web resources with the help of annotations, classes and interfaces provided by JAX-RS API. Since this depends heavily on annotations, JAX-RS requires Java version 5 and above. JAX-RS applications are bundled in a WAR file similar to any other web application in Java and deployed on a container that supports Servlet. A Servlet provided by the vendor is used for routing the requests to the appropriate web resource. One of the goals of JAX-RS is to make the web resources deployable across different types of web containers achieving portability.

Developing RESTful Web Services using JAX-RS

RESTful Web Services in Java can be created with the help of JAX-RS with any implementation. The classes and interfaces used for creating RESTful web services using JAX-RS are available in the following packages:




Resource Class

JAX-RS allows POJOs to expose the functionality as web services through annotations. According to JAX-RS, a 'Resource' is a Java class (POJO) which uses JAX-RS annotations to implement a web resource. Annotation @Path is used on a Java class to identify that as a Resource class.

This class is packaged as part of a WAR file with an entry for the Servlet provided by JAX-RS implementation (Jersey for example). Then the WAR file is deployed in the container (Glassfish for example) that provides support for JSR 311. Then the resource is accessed using the normal URI http://hostname:portnumber/WARfilename/stockquote.

This will result in invocation of this class annotated with @Path annotation with a value matching to the corresponding value of the URI.

Life Cycle of Resource Class

A new instance of the resource class will be created for each request to that resource. The life cycle of the resource starts with the creation of object, the constructor is invoked and then the required dependencies are injected. The appropriate resource method is invoked and once the response is provided, the object is made available for garbage collection.



S Sangeetha
S Sangeetha

What do you think?

JAX Magazine - 2014 - 06 Exclucively for iPad users JAX Magazine on Android


Latest opinions