Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
by Nader Albert









What is ReST?
History of ReST
Key General Concepts
Key ReST Principles
ReST Maturity Model
Caching
Genera...


Representational State Transfer.



Transferring the State of a Resource in a Specific
Representation.



ReST is an ...


Architectural Styles and the Design of Network-based
Software Architectures, doctoral dissertation by Roy Fielding.


...
•
•
•

Resource Oriented Architecture
Server and Client Decoupled
Resource Representation
Resource Based instead of Action Based
Things instead of Actions
Nouns instead of Verbs
ROA

Decoupling

Resource Represen...
ROA

Decoupling

Resource Representation






Separate
the
Representation

Resource

from

Resource
has
one
structure
multiple representations

its

and

State...
•
•
•
•
•

Unique Identifier
Link Things together
Use Standard Methods
Multiple Representations
Communicate Statelessly


Use URIs to identify everything that merits
being identifiable.
Collections
of things

One thing

Unique Identifier

Li...
Orde
r

Payment

Customer
Item

Unique Identifier

Item

Link Together

Standard Methods

Multiple Representations

Statel...
Unique Identifier

Link Together

Standard Methods

Multiple Representations

Stateless


Isolate Representation from Structure



Separation of concerns (framework understands format, application
understands...


Server doesn’t retain state for any client beyond a single request



Request encapsulates all necessary information

...
•
•
•
•
•
•

RestBucks Example
Richardson’ Maturity Model
POX Services
URI Tunnelling
CRUD Web Services
Hypermedia Web Ser...
Not Yet There!


Plain Old XML over HTTP.



Uses HTTP requests and responses to transfer
documents between a client and a server.



...






Application semantics reside inside the XML payload while
HTTP metadata is ignored.
Would work just as well over ...
Method
Invocation
Order
Information,
embedded in
the XML payload

http://restbucks.com/placeOr
der

POX Services

URI Tunn...




Falls under the POX umbrella.
An attempt to standardize the way in which
information is represented in the HTTP
requ...
<method-call>
<method-name>PlaceOrder</method-name>
<params>
<param> <value> <string>1234</string> </value> </param>
<para...
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn
<soap:E...
We Are Here!






HTTP only used as a transport protocol to move
URI’s across the network.

Uses URIs to transfer information acros...




URIs are used to encode operations rather than
identify resources.

Order Information,
embedded in the URI

Tempting...
We Are Here!


Embrace HTTP as an application protocol.

Level Two



Use URI’s to identify resources rather than methods.



Embrac...


Service Contract is simple, concise, and
intuitive.

POX Services

URI Tunneling

CRUD Web Services

Hypermedia
2xx Success

3xx Redirection

4xx Client Error

5xx Server Error

POX Services

URI Tunneling

CRUD Web Services

Hypermed...
POX Services

URI Tunneling

CRUD Web Services

Hypermedia


POST is used as a request to create a resource.



Payload contains a representation of the resource.

POX Services

U...


201 Created

(Request has been accepted)


400 Bad Request

(Request has been rejected)

◦ Client sends malformed order request to the server
◦ Client shouldn’t r...


500 Internal Server Error
◦ Indicates a server fault and inability to recover internally
◦ Try Again Later.

◦ Client c...


503 Service Unavailable
◦ Indicates the server is temporarily too busy to process the request
◦ Has to be associated wi...


GET is used to retrieve a particular representation of a resource.



Usually follows a POST request.



Use Location...


200 OK / 204 No-Content (Request has been accepted)

POX Services

URI Tunneling

CRUD Web Services

Hypermedia


404 NOT Found

(Request has been rejected)

Read Order Request

POX Services

Read Order 404 Response

URI Tunneling

C...


500 Internal Server Error

◦ Consumer can retry the request after while (GET is idempotent)
◦ Either wait till the serv...


PUT is used to update a resource.



State of the resource is updated to reflect the data in the request
representatio...


200 OK / 204 No Content

Update Order Request

(Request has been accepted)

OK Response

No Content Response



409 Conflict / 405 Method Not Allowed (Request has been rejected)
Choosing between 409 and 405 depends on the use cas...


500 Internal Server Error


(Request has been rejected)

Consumer can safely repeat the operation (PUT is idempotent)
...


DELETE is used as a request to delete a resource.



It can be used to cancel an order and not physically delete
it.

...


Helps to align Resources State in multi-consumer scenarios.



The server hashes the resource representation in the re...
POX Services

URI Tunneling

CRUD Web Services

Hypermedia
Just Arrived!




Plain
XML
representation
leaves
the
consumer without a guide to successfully
complete the business transaction it ha...






Hypermedia As The Engine Of Application
State
By Transiting links between resources, we
change the state of an ap...






A way for the service to enforce its Domain Application
Protocol (DAP).
Convey protocol information in a d e c l ...


Specifies legal interactions between a consumer and a set of resources
involved in a business process.

POX Services

U...


Custom format would require a custom Media Type.



Specify Custom Media Type via the Content-Type HTTP Header.



Me...






Application Custom Media Type:
(application/vnd.restbucks+xml)
The vnd.restbucks sub type marks the payload as be...
POX Services

URI Tunneling

CRUD Web Services

Hypermedia
<order xmlns=″http://schemas.restbucks.com″
xmlns:dap=″http://schemas.restbucks.com/dap″>
<dap:link mediaType=″application...
POX Services

URI Tunneling

CRUD Web Services

Hypermedia


Reduce Bandwidth



Reduce Latency



Reduce Load



Hide Network Failure



Reduce the statelessness problem






Responses to GET requests are cacheable by default.
Responses to POST requests are not cacheable by default, but
...


Expires

◦ Can be used in the Response
◦ Specifies an absolute expiry time for a cached representation.
◦ Beyond that t...
Cacheable
response.

Response is
never cached

Remains fresh
for 1 hour
Used by the
Cache Server for
revalidation

Cacheab...


URI’s should be intuitive and not magical.



Use nouns and not verbs.



Follow the safe / idempotent rule.



Uniq...


ReST may not fit well in every situation



URI Design is quite challenging.



Requires deep understanding of the do...






REST in Practice: Hypermedia and Systems Architecture, Jim
Webber, et al., 2010, O’Reilly Media, inc.
http://www...
ReST
ReST
ReST
ReST
ReST
ReST
ReST
Upcoming SlideShare
Loading in …5
×

ReST

997 views

Published on

Provides an introduction to the principles of Representational State Transfer (ReST), as an architectural style. Includes a comparison between the different alternatives of application to application communications. Explains the Richardson's maturity model, and shed light on the different degrees of maturity, with an emphasis on the Hypermedia services and the concepts of HATEOAS, and how Hypermedia services can participate in driving the Domain Application Protocol.

Published in: Technology
  • Be the first to comment

ReST

  1. 1. by Nader Albert
  2. 2.         What is ReST? History of ReST Key General Concepts Key ReST Principles ReST Maturity Model Caching General Tips Key Issues and Challenges
  3. 3.  Representational State Transfer.  Transferring the State of a Resource in a Specific Representation.  ReST is an Architectural Style.  Client – Server Architecture.  Leveraging the application protocol capabilities of HTTP.
  4. 4.  Architectural Styles and the Design of Network-based Software Architectures, doctoral dissertation by Roy Fielding.  Inspired by the architecture of the World Wide Web  HTTP misconceived as a Transport Protocol, while it is not just that.  Leveraging HTTP capabilities gives straightforward application-to-application connectivity with very little effort.
  5. 5. • • • Resource Oriented Architecture Server and Client Decoupled Resource Representation
  6. 6. Resource Based instead of Action Based Things instead of Actions Nouns instead of Verbs ROA Decoupling Resource Representation
  7. 7. ROA Decoupling Resource Representation
  8. 8.    Separate the Representation Resource from Resource has one structure multiple representations its and State Structure Format Resource can be represented in different formats ROA Decoupling Resource Representation
  9. 9. • • • • • Unique Identifier Link Things together Use Standard Methods Multiple Representations Communicate Statelessly
  10. 10.  Use URIs to identify everything that merits being identifiable. Collections of things One thing Unique Identifier Link Together Standard Methods Multiple Representations Stateless
  11. 11. Orde r Payment Customer Item Unique Identifier Item Link Together Standard Methods Multiple Representations Stateless
  12. 12. Unique Identifier Link Together Standard Methods Multiple Representations Stateless
  13. 13.  Isolate Representation from Structure  Separation of concerns (framework understands format, application understands semantics) Client Server supplies the format requests a format XML,HTML,TEXT,CSV, JSON GET / order/1234 HTTP /1.1 Host: restbucks.com Accept: a p p l i c a t i o n / j s o n Unique Identifier Link Together XML,HTML,TEXT,CSV, JSON HTTP/1.1 200 OK Content-Length: 260 Content-Type: a p p l i c a t i o n / j s o n Standard Methods Multiple Representations Stateless
  14. 14.  Server doesn’t retain state for any client beyond a single request  Request encapsulates all necessary information   Number of clients impacts the server’s footprint if it keeps client state Client doesn’t have to talk to the same server in two consecutive requests Unique Identifier Link Together Standard Methods Multiple Representations Stateless
  15. 15. • • • • • • RestBucks Example Richardson’ Maturity Model POX Services URI Tunnelling CRUD Web Services Hypermedia Web Services
  16. 16. Not Yet There!
  17. 17.  Plain Old XML over HTTP.  Uses HTTP requests and responses to transfer documents between a client and a server.  Uses HTTP POST to transfer XML documents between systems.  Uses the URI, as an i d e n t i f i e r to a r e m o t e method POX Services URI Tunneling CRUD Web Services Hypermedia
  18. 18.    Application semantics reside inside the XML payload while HTTP metadata is ignored. Would work just as well over TCP connection. Platform independence, and ubiquitous connectivity between systems. POX Services URI Tunneling CRUD Web Services Hypermedia
  19. 19. Method Invocation Order Information, embedded in the XML payload http://restbucks.com/placeOr der POX Services URI Tunneling CRUD Web Services Hypermedia
  20. 20.   Falls under the POX umbrella. An attempt to standardize the way in which information is represented in the HTTP request and response payloads. POX Services URI Tunneling CRUD Web Services Hypermedia
  21. 21. <method-call> <method-name>PlaceOrder</method-name> <params> <param> <value> <string>1234</string> </value> </param> <param> <array> <struct> <member> <name>Name</name> <value><string>latte</string></value> </member> <member> <name>Quantity</name> <value><int>1</int></value> </member> </struct> </array> </param> </params> </method-call> POX Services URI Tunneling CRUD Web Services Hypermedia
  22. 22. POST /InStock HTTP/1.1 Host: www.example.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPrice> <m:StockName>XYZ</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope> POX Services URI Tunneling CRUD Web Services Hypermedia
  23. 23. We Are Here!
  24. 24.    HTTP only used as a transport protocol to move URI’s across the network. Uses URIs to transfer information across system boundaries by encoding information within the URI itself. The content of a URI, defines the piece of code to be executed and parameterizes it. POX Services URI Tunneling CRUD Web Services Hypermedia
  25. 25.   URIs are used to encode operations rather than identify resources. Order Information, embedded in the URI Tempting to violate the safe / idempotency rules. POX Services URI Tunneling CRUD Web Services Hypermedia
  26. 26. We Are Here!
  27. 27.  Embrace HTTP as an application protocol. Level Two  Use URI’s to identify resources rather than methods.  Embrace HTTP (verbs , codes and Media Types).  Supports basic operations (create, read, update, delete)  Client retrieves Resource, investigates Media Type to decode Body POX Services URI Tunneling CRUD Web Services Hypermedia
  28. 28.  Service Contract is simple, concise, and intuitive. POX Services URI Tunneling CRUD Web Services Hypermedia
  29. 29. 2xx Success 3xx Redirection 4xx Client Error 5xx Server Error POX Services URI Tunneling CRUD Web Services Hypermedia
  30. 30. POX Services URI Tunneling CRUD Web Services Hypermedia
  31. 31.  POST is used as a request to create a resource.  Payload contains a representation of the resource. POX Services URI Tunneling CRUD Web Services Hypermedia
  32. 32.  201 Created (Request has been accepted)
  33. 33.  400 Bad Request (Request has been rejected) ◦ Client sends malformed order request to the server ◦ Client shouldn’t retry to submit the same order.
  34. 34.  500 Internal Server Error ◦ Indicates a server fault and inability to recover internally ◦ Try Again Later. ◦ Client can try to GET the current representations of another resources (GET is safe) ◦ Server –if in a recoverable state- cleans up any state created by the failed interaction, to keep its own internal order state consistent. POX Services URI Tunneling CRUD Web Services Hypermedia
  35. 35.  503 Service Unavailable ◦ Indicates the server is temporarily too busy to process the request ◦ Has to be associated with Retry-After header. POX Services URI Tunneling CRUD Web Services Hypermedia
  36. 36.  GET is used to retrieve a particular representation of a resource.  Usually follows a POST request.  Use Location header in a POST response, to retrieve the current state of a created resource POX Services URI Tunneling CRUD Web Services Hypermedia
  37. 37.  200 OK / 204 No-Content (Request has been accepted) POX Services URI Tunneling CRUD Web Services Hypermedia
  38. 38.  404 NOT Found (Request has been rejected) Read Order Request POX Services Read Order 404 Response URI Tunneling CRUD Web Services Hypermedia
  39. 39.  500 Internal Server Error ◦ Consumer can retry the request after while (GET is idempotent) ◦ Either wait till the service comes back, or accept to hand over control to an out-of-band mechanism. POX Services URI Tunneling CRUD Web Services Hypermedia
  40. 40.  PUT is used to update a resource.  State of the resource is updated to reflect the data in the request representation POX Services URI Tunneling CRUD Web Services Hypermedia
  41. 41.  200 OK / 204 No Content Update Order Request (Request has been accepted) OK Response No Content Response
  42. 42.   409 Conflict / 405 Method Not Allowed (Request has been rejected) Choosing between 409 and 405 depends on the use case 409 Response
  43. 43.  500 Internal Server Error  (Request has been rejected) Consumer can safely repeat the operation (PUT is idempotent) POX Services URI Tunneling CRUD Web Services Hypermedia
  44. 44.  DELETE is used as a request to delete a resource.  It can be used to cancel an order and not physically delete it. POX Services URI Tunneling CRUD Web Services Hypermedia
  45. 45.  Helps to align Resources State in multi-consumer scenarios.  The server hashes the resource representation in the response.  Consumer are recommended to perform subsequent conditional requests through the (If-Match / If-Not-Match request header)  Saves Server-Side Computations  Out of synch state, and 409 error codes, are less frequent. POX Services URI Tunneling CRUD Web Services Hypermedia
  46. 46. POX Services URI Tunneling CRUD Web Services Hypermedia
  47. 47. Just Arrived!
  48. 48.   Plain XML representation leaves the consumer without a guide to successfully complete the business transaction it has initiated. Guides may include: ◦ Written documentation. ◦ Static contracts such as WADL, or URI templates. ◦ Hypermedia (expose entry point URI’s and let Hypermedia take over) ex: http://restbucks.com/order POX Services URI Tunneling CRUD Web Services Hypermedia
  49. 49.    Hypermedia As The Engine Of Application State By Transiting links between resources, we change the state of an application (world wide web). Application state is a snapshot of the entire system at a particular instant. POX Services URI Tunneling CRUD Web Services Domain Application Protocol Order Created Order Delivered Order Ready Hypermedia Order Updated Order Paid
  50. 50.    A way for the service to enforce its Domain Application Protocol (DAP). Convey protocol information in a d e c l a r a t i v e and j u s t i n - t i m e fashion as part of an application’s resource representations. Sits atop HTTP and narrow HTTP’s broad application protocol capabilities to support specific business goals. POX Services URI Tunneling CRUD Web Services Hypermedia
  51. 51.  Specifies legal interactions between a consumer and a set of resources involved in a business process. POX Services URI Tunneling CRUD Web Services Hypermedia
  52. 52.  Custom format would require a custom Media Type.  Specify Custom Media Type via the Content-Type HTTP Header.  Media type value indicates the service’s preferred scheme for  interpreting a representation. Media type specification sets out the formats (and any schemas), processing model and hypermedia controls that services will embed in representations. POX Services URI Tunneling CRUD Web Services Hypermedia
  53. 53.    Application Custom Media Type: (application/vnd.restbucks+xml) The vnd.restbucks sub type marks the payload as being part of Restbucks’ DAP. The generic application/xml would give wrong indication, and lead to skipping hypermedia benefits. POX Services URI Tunneling CRUD Web Services Hypermedia
  54. 54. POX Services URI Tunneling CRUD Web Services Hypermedia
  55. 55. <order xmlns=″http://schemas.restbucks.com″ xmlns:dap=″http://schemas.restbucks.com/dap″> <dap:link mediaType=″application/vnd.restbucks+xml″ rel=″http://relations.restbucks.com/cancel″/> <dap:link mediaType=″application/vnd.restbucks+xml″ rel=″http://relations.restbucks.com/payment″/> <dap:link mediaType=″application/vnd.restbucks+xml″ rel=″http://relations.restbucks.com/update″/> <dap:link mediaType=″application/vnd.restbucks+xml″ uri=″http://restbucks.com/order/1234″ uri=″http://restbucks.com/payment/1234″ uri=″http://restbucks.com/order/1234″ uri=″http://restbucks.com/order/1234″ rel=″self″/> <location>takeaway</location> <items> <item> <name>latte</name> <qty>2</qty> <milk>skimmed</milk> <size>small</size> </item> </items> <status>unpaid</status> </order> POX Services URI Tunneling CRUD Web Services Hypermedia
  56. 56. POX Services URI Tunneling CRUD Web Services Hypermedia
  57. 57.  Reduce Bandwidth  Reduce Latency  Reduce Load  Hide Network Failure  Reduce the statelessness problem
  58. 58.    Responses to GET requests are cacheable by default. Responses to POST requests are not cacheable by default, but can be made cacheable. Responses to PUT and DELETE requests are not cacheable at all.
  59. 59.  Expires ◦ Can be used in the Response ◦ Specifies an absolute expiry time for a cached representation. ◦ Beyond that time, a cached representation is considered stale and must be revalidated with the origin server.  Cache-Control ◦ Can be used in the Request and the Response ◦ Comprises one or more directive. ◦ Determines whether a response is cacheable, and if so, by whom, and for how long.
  60. 60. Cacheable response. Response is never cached Remains fresh for 1 hour Used by the Cache Server for revalidation Cacheabl e response. Remains fresh until this time
  61. 61.  URI’s should be intuitive and not magical.  Use nouns and not verbs.  Follow the safe / idempotent rule.  Unique identifiers should come as part of the URI.
  62. 62.  ReST may not fit well in every situation  URI Design is quite challenging.  Requires deep understanding of the domain model structure.  Extensive query parameters remains a problem.
  63. 63.     REST in Practice: Hypermedia and Systems Architecture, Jim Webber, et al., 2010, O’Reilly Media, inc. http://www.infoq.com/minibooks/emag-03-2010-rest http://www.infoq.com/articles/roa-rest-of-rest http://www.restapitutorial.com/

×