RESTful Applications Basics

REpresentational State Transfer (REST) describes an architectural style and design principles for creating client-server web and mobile applications. REST is based on web standards (HTTP), treats all data sources as resources, and uses the run time annotations implemented in Jersey to simplify application development and deployment.

Jersey: Jersey is the reference implementation for JAX-RS (JSR-311). It provides a REST framework with APIs that extend the JAX-RS toolkit with features and utilities that further simplify RESTful service and client development. Jersey implements support for the run time annotations defined in JSR 311, and has  a REST client library that you can use for testing or building a real client in Java.

Resources: Resources embody the application data. They can be text files, HTML pages, images, videos, or dynamic business data that the web service accesses through its data classes. A REST web service provides access to the resources, and a REST client accesses and modifies the resources.

Annotations: Every resource is identified by a global ID (URI) that is specified in the web service code with @Path annotations before the relevant class methods.

The web service must pass the requested resource to the client in a format that the client can parse. Many web services represent resources in either XML or JSON formats. You specify how to represent the resources with the @Produces annotation before the relevant class methods as follows:

  • XML: @Produces(MediaType.APPLICATION_XML, or
  • JSON: @Produces(MediaType.APPLICATION_JSON)

All resources are accessed through an HTTP interface that recognizes standard HTTP methods. You specify which HTTP method to use with annotations before the relevant class methods. For example, a class that gets information is preceded by the @GET annotation, and a class that changes information is preceded by the @POST annotation. See Java API for RESTful Web Services for information about the supported HTTP operations.

Example: The following method is annotated to get seller names in JSON format and to use the following URI, where SellerStatus is the package name for the web service code and resources:

public class SellerService {

   SellerDataObject sellerdo = new SellerDataObject();

   public List<Seller> getSellers(){
      return sellerdo.getAllSellers();

HTTP Communications

The client sends a message (HTTP Request) and the server responds with a message (HTTP Response).

The client requests an action (GET, POST, DELETE, etc.) and provides the URI to the resource on which it wants to act (for example, http://localhost:8080/SellerStatus/rest/SellerService/sellers).

The server responds with the server status for the requested resource. For example, 200 if the resource is available or 404 if the requested resource cannot be found. See List of HTTP Status Codes for more information. If the resource is available, the server includes the resource in the response.

Statelessness: A RESTful web service does not store information about the client state on the server.  This means your application design is simpler and works seamlessly with HTTP, which is a stateless protocol. Statelessness means that each client request is an independent request.

However, client statelessness means the web service might need to get client context information in each client request and do some interpretation to process further requests from that client.

Caching: The client can store (cache) the server response to avoid requesting the same information over and over. See List of HTTP header fields for server response headers and the particulars about the cache-control server response header.

Security: Validate all server inputs, use session-based authentication, never put sensitive data in a URL, restrict method execution, check for well-formed input, and use HTTP status codes to indicate the access status of a page.

RESTful Applications Basics

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s