Friday, 15 November 2013

Rest API


Rest API

 

Introduction


REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.
 REST service is:
·         Platform-independent (you don't care if the server is Unix, the client is a Mac, or anything else),
·         Language-independent (C# can talk to Java, etc.),
·         Standards-based (runs on top of HTTP),
 and
·         Can easily be used in the presence of firewalls.



REST-Architectural style



The REST architectural style describes the following  constraints applied to the architecture, while leaving the implementation of the individual components free to design:

Client–server:-

A uniform interface separates clients from servers. Servers are not concerned with the user interface or user state, so that servers can be simpler and more scalable. Servers and clients may also be replaced and developed independently, as long as the interface between them is not altered.

Stateless:-

There is no connection state; interaction is stateless (although the servers and resources can of course be stateful). Each new request should carry all the information required to complete it, and must not rely on previous interactions with the same client.
A stateless protocol does not require the server to retain session information or status about each communications partner for the duration of multiple requests.

Cacheable:-

A web cache is a mentorism for the temporary storage (caching) of web documents, such as HTML pages and images, to reduce bandwidth usage, server load, and perceived lag. A web cache stores copies of documents passing through it; subsequent requests may be satisfied from the cache if certain conditions are met.
Resources should be cachable whenever possible (with an expiration date/time). The protocol must allow the server to explicitly specify which resources may be cached, and for how long.
·         Since HTTP is universally used as the REST protocol, the HTTP cache-control headers are used for this purpose.
·         Clients must respect the server's cache specification for each resource.

Layered system:-

A layered system is a system in which components are grouped, i.e., layered, in a hierarchical arrangement, such that lower layers provide functions and services that support the functions and services of higher layers.
A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way. Intermediary servers may improve system scalability by enabling load-balancing and by providing shared caches. They may also enforce security policies.
The system has a client-server, but of course one component's server can be another component's client.

Uniform interface:-

The uniform interface that any REST interface must provide is considered fundamental to the design of any REST service

Identifications of resources:

Individual resources are identified in requests, for example using URIs in web-based REST systems. The resources themselves are conceptually separate from the representations that are returned to the client.

Manipulation of resources through these representations


When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource on the server, provided it has permission to do so.

Self-descriptive messages


Each message includes enough information to describe how to process the message. For example, which parser to invoke may be specified by an Internet media type (previously known as a MIME type). Responses also explicitly indicate their cacheability.



Java frameworks for RESTful Web service



Two Java frameworks have emerged to help with building RESTful Web services.
1-Restlet framework
2-Jersey framework



Restlet framework



It implements concepts such as resources, representation, connector, and media type for any kind of RESTful system, including Web services. In the Restlet framework, both the client and server are components. Components communicate with each other through connectors. The most important classes in the framework are the abstract class Uniform and its concrete subclass Restlet, the subclasses of which are specialized classes such as Application, Filter, Finder, Router, and Route. Those subclasses work together to handle authentication, filtering, security, data transformation, and routing the incoming requests to the respective resources. The Resource class generates the presentation for the client.



Jersey framework



JSR-311 is a specification from Sun Microsystems that defines a set of Java APIs for the development of RESTful Web services.
 Jersey is the reference implementation for JSR-311.
JSR-311 provides a set of annotations with associated classes and interfaces that can be used to expose Java objects as Web resources. The specification assumes HTTP is the underlying network protocol. It provides clear mappings between the URI and corresponding resource classes, and mappings between HTTP methods with the methods in Java objects, by using annotations. The API supports a wide range of HTTP entity content types including HTML, XML, JSON, GIF, JPG, and so on. It will also provide the needed plug-in ability to allow additional types to be added by an application in a standard manner.

Developing RESTful Web Services with JAX-RS



JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture.
The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.
Annotation
Description
@Path
The @Path annotation’s value is a relative URI path indicating where the Java class will be hosted: for example, /helloworld. You can also embed variables in the URIs to make a URI path template. For example, you could ask for the name of a user and pass it to the application as a variable in the URI: /helloworld/{username}.
@GET
The @GET annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP GET requests.
@POST
The @POST annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP POST requests.









@PathParam
The @PathParam annotation is a type of parameter that you can extract for use in your resource class. URI path parameters are extracted from the request URI, and the parameter names correspond to the URI path template variable names specified in the @Path class-level annotation.
@QueryParam
The @QueryParam annotation is a type of parameter that you can extract for use in your resource class. Query parameters are extracted from the request URI query parameters.




@Produces
The @Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client: for example, "text/plain".




















No comments:

Post a Comment