Friday, March 22, 2019

RESTful Web services


RESTful Web services


Persistent communication, on the other hand, stores the message until the recipient receives it. A typical example of asynchronous persistent communications Message-Oriented Middleware (MOM). Message-oriented middleware is also called a message-queuing system, a message framework, or just a messaging system.

Message oriented communication vs recourse oriented communication
attribute
Object-oriented
Resource-oriented
Granularity
Object instance
Resource oriented
Main focus
Marshalling parameter values
Request addressing
Addressing / request routing
Routed to unique object instance
Unique address per recourse
Are replies cacheable ?
No
Yes
Application interface
Specific to this object / class-description middleware specific
Generic to the request mechanism
Payload / data format description
Yes – usually middleware specific
No – nothing directly linked address/URL


The resource based nature of the REST style
Resources. The fundamental concept in any Restful API is the resource. A resource is an object with a type, associated data, relationships to other resources, and a set of methods that operate on it. ... Collections are themselves resources as well.
REST architecture treats every content as a resource. These resources can be Text Files, Html Pages, Images, Videos or Dynamic Business Data. REST Server simply provides access to resources and REST client accesses and modifies there sources.
•Things vs Actions
•Nouns vs Verbs
•Identified by URIs
•Multiple URIs may refers to same resource
But different according to the HTTP verb used (as like CRUD operation on same student resource)
•Separate from their representations
Resource may be represented as request content type either JSON or XML.

Representations in REST style
•How resources get manipulated and presented
•Part of the resource state is transferred between client and server
•Typically JSON or XML
•Or even can be a web page, file, image
E.g. The state of a student (as per the database) can be represented by a resource of webpage, XML dataset.
A REST API commonly uses a response message's entity body to help convey the state of a request message's identified resource. REST APIs often employ a text-based format to represent a resource state as a set of meaningful fields. Today, the most commonly used text formats are XML and JSON.
In REST-speak, a client and server exchange representations of a resource, which reflect its current state or its desired state. REST, or Representational state transfer, is a way for two machines to transfer the state of a resource via representations.
This is exactly how you browsing the web works. An html page isn’t a resource , its just one representation . And when we submit a form, we’re just sending different representation back to the server.
One resource could have many representation. An API where you’re able to request the XML,JSON OR HTML representation of any resources. A representation is a machine readable explanation of the current state of a resource 


The constraints of REST
1)     Client-server
 •The REST is for explicitly for networked distributed systems, which are based on the client-server style.
2)     Layered System
•A client doesn’t need to know 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.
•Layers may also enforce security policies.
3)     Stateless
•One client can send multiple requests to the server.
•Each request is independent.
 •Every request must contain all the necessary information so that the server can understand it and process it accordingly.
•The server must not hold any information about the client state.
•Any state information must stay on client –such as sessions.
4)     Cacheable
•As many clients access the same server, often they may request the same resources.
•It is necessary that these responses might be cached, avoiding unnecessary processing, which may affect performance.
5)     Code-On-Demand (Optional)
•This allows the customer to run some code on demand, that is, extend part of server logic to the client, either through an applet or scripts.
6)     Uniform Interface
•Defines the interface between clients and servers.
•It simplifies and decouples the architecture, which enables each part to evolve independently
•REST is defined by four interface constraints:
o   Identification of resources
o   Manipulation of resources through representations
o   Self-descriptive messages
o   Hypermedia as the engine of application state
Uniform Interface –Identification of resources
•Based on the web’s Request-Response model
•Request = URL + HTTP Verbs (GET,POST,PUT,DELETE)
HTTP/1.1 GET http://abc.com/user/John
•Response = HTML, XML, JSON, TXT

Contemporary examples of different types of implementations for the elements of REST style
Components
•Software that interacts with one another.
•Communicate by transferring representations of resources through a standard interface rather than operating directly upon there source itself.
•Used to access, provide access to, or mediate access to resources.

Component
Examples
User agent
Browser-based-application
Origin server
Server-side application
Proxy
CERN Proxy, Netscape Proxy, Gauntlet
Gateway
Squid, CGI, Reverse Proxy

Connector
•Represent activities involved in accessing resources and transferring representations.
•REST encapsulates different activities of accessing and transferring representations into different connector types.
•Connectors are abstract interfaces for component communication, enhancing simplicity by hiding the underlying implementation of resources and communication mechanisms.
Connector
Examples
Client
Browser
Server
Web Server
Cache
Browser cache, Akamai cache network
Resolver
DNS lookup, DOI lookup
Tunnel
SOCKS, SSL after HTTP CONNECT

Data
•Key aspect of REST is the state of the data elements, its components communicate by transferring representations of the current or desired state of data elements
 •REST manages data in the following ways:
o   Render the data(traditional client-server style) where it is located and send a fixed-format image to the recipient.
o   Encapsulate the data(mobile object style) with a rendering engine and send both to the recipient or,
o   Send the raw data to the recipient along with metadata that describes the data type, so that the recipient can choose their own rendering engine.

Data
Examples
Resource
Conceptual target of a hypertext reference
Resource Identifier
URL, URN
Representation
HTML document, JPEG image, XML dataset
Representation metadata
Media type, last-modified time
Resource metadata
Source link, alternates
Control data
if-modified-since, cache-control

Request = URL + HTTP Verbs (GET,POST,PUT,DELETE)
•URL is the key technique in RESTful communication
•The API of the RESTful service is a set of URLs
o   The resource is determined by the URL segments
o   The CRUD operations are determined by the HTTP verb
Operation=Read, Verb=GET
•Collection
o   SLIIT.com/students/
o   SLIIT.com/students/DS
o   SLIIT.com/students/DS/IT123456/posts
•Single item
o   SLIIT.com/students/IT123456
o   Blog.com/posts/pid15948
•Multiple items
o   SLIIT.com/students/IT123456;IT456456;IT998877



Operation=Create, Verb=POST
•Single item (single item in the payload)
o   SLIIT.com/students/
o   SLIIT.com/students/DS
o   SLIIT.com/students/DS/IT123456/posts
 •Multiple items
o   Same URL, items will be in the payload
Operation=Update, Verb=PUT
•Collection (data in the payload)
o   SLIIT.com/students/
o   SLIIT.com/students/DS
o   SLIIT.com/students/DS/IT123456/posts
•Single item
o   SLIIT.com/students/IT123456
o   Blog.com/posts/pid15948
•Multiple items
o   SLIIT.com/students/IT123456;IT456456;IT998877
Operation=Delete, Verb=DELETE
•Collection
o   SLIIT.com/students/
o   SLIIT.com/students/DS
o   SLIIT.com/students/DS/IT123456/posts
•Single item
o   SLIIT.com/students/IT123456
o   Blog.com/posts/pid15948
•Multiple items
o   SLIIT.com/students/IT123456;IT456456;IT998877




JAX -RS
JAX -RS is an API
 •There are two implementations





Well the most important reason , I think to use spring for developing restful web service is that you can use your spring mvc experience for developing RESTFUL web service and you don’t need to learn a new framework or library , which means you can quickly roll out your REST API
Spring has excellent support for developing RESTFul web services. In last couple of version starting from Spring version 3.0 , its provide a lot of enhancement to Spring MVC  to provide first-class REST support. It provides dedicated annotations to make the development of RESTful resources  even easier .
Its also not only help you to create RESTful web services but also provide classes to consume REST resources .
JAX-RS: JAVA API for  RESTful web service is a java programming API spec that provides support in creating web service according to the REST architecture pattern . JAX-RS uses annotations , introduced JAVA SE 5 , to simplify the development and deployment of web service clients and endpoints


Annotations in the JAX-RS API are used to provide meta-data around the web resource.
The Goals of JAX – RS API  are :

                                

·        Annotattion defines the URI or the relative path of the JAX-RS method.
·        Annotation is used to bind the parameter passed in the paratameter passed in the URI to the parameter in the REST method.
·        Annotation specifies that the HTTP GET request shall be served by this method.
·        Annotation specifies that the HTTP POST request shall be served by this method.
·        Annotation is used to extract out query parameters from the request URI.