SlideShare ist ein Scribd-Unternehmen logo
1 von 149
REST




REST
Theory and Practice




Vladimir Tsukur / Design Engineer & Team Lead @GlobalLogic, Partner @FrostDigital   2012
REST




 1
Theory

                2
REST




REpresentational State Transfer          3
REST




REpresentational State Transfer          4
REST




«A software architecture is an abstraction
   of the run-time elements of a software
system during some phase of its operation»


                                   Roy T. Fielding, 2000


 REpresentational State Transfer                          5
REST




Architecture Elements

               Components
               Components
                Components

                                  Data



                Connectors




REpresentational State Transfer                 6
REST




Architecture Elements


                                  SQL


                      DAO               DB




REpresentational State Transfer                     7
REST




«An architectural style is a coordinated set
of architectural constraints that restricts the
roles/features of architectural elements and
   the allowed relationships among those
    elements within any architecture that
           conforms to that style»

                                   Roy T. Fielding, 2000


 REpresentational State Transfer                          8
REST




               REST = style




REpresentational State Transfer          9
REST




It is actually a hybrid style ;)




REpresentational State Transfer          10
REST




    WWW Requirements First

‣    Low Entry-barrier      ‣         Hypermedia
‣    Extensibility          ‣         Prepare for change
‣    Distributed Hypermedia ‣         Large-grain data transfer
‣    Internet-scale         ‣         Anarphic scalability
                            ‣         Independent deployment




    REpresentational State Transfer                                 11
REST




                        REST
                      Constraints


REpresentational State Transfer            12
REST




1. Client - Server




+ Separation of concerns
+ UI portability
+ Improved scalability

REpresentational State Transfer          13
REST




2. Stateless




+ Visibility                      - Performance
+ Reliability                     - Consistency
+ Scalability

REpresentational State Transfer                          14
REST




3. Cache




+ Efficiency                       - Reliability
+ Scalability
+ UP Performance

REpresentational State Transfer                          15
REST




Client-Cache -
Stateless-Server =



                                  <
                                  1994


REpresentational State Transfer                 16
REST




Web < 1994

- Static documents
- CERN libwww common library
- No consistent set of semantics
for all resources



REpresentational State Transfer           17
REST




4. Uniform Interface




+ Simplicity                      - Efficiency
+ Visibility
+ Evolvability

REpresentational State Transfer                        18
REST




5. Layered System




+ Simplicity                      - UP Performance
+ Scalability
+ Evolvability

REpresentational State Transfer                       19
REST




6. [Code-on-Demand]




+ Client Simplicity               - Visibility
+ Extensibility


REpresentational State Transfer                         20
REST




REST




REpresentational State Transfer          21
REST




                  REST
              Data Elements


REpresentational State Transfer          22
REST




Resources




REpresentational State Transfer          23
REST




Resources




      <a href=’                   ’>Winnie</a>



REpresentational State Transfer                         23
REST




«A resource is a conceptual mapping to a
     set of entities, not the entity that
    corresponds to the mapping at any
         particular point in time.»


                                  Roy T. Fielding, 2000


REpresentational State Transfer                          24
REST




Resources

                      => {}
                      => static:


                      => dynamic:



REpresentational State Transfer            25
REST




Resources

                                  M R(t)={
                                      ids and/or

                                      representations



                                  }



REpresentational State Transfer                                26
REST




Resources




            1.0                   1.1   2.0   «latest»




REpresentational State Transfer                             27
REST




Resources




            1.0                   1.1   2.0 != «latest»




REpresentational State Transfer                              27
REST




Resources

      + Generality
      + Late binding of reference to
               implementation (request-time)
      +        Allows to reference concept, not
               some singular representation


REpresentational State Transfer                     28
REST




Resource IDs




REpresentational State Transfer          29
REST




Resource IDs


 http://www.teddybear.org/winnie




REpresentational State Transfer          29
REST




Resource IDs
   «Since centralized link servers are an
anathema to the immense scale and multi-
 organizational domain requirements of the
  Web, REST relies instead on the author
choosing a resource identifier that best fits
the nature of the concept being identified.»

                                  Roy T. Fielding, 2000


REpresentational State Transfer                          30
REST




Resource Representations
 Content-Type: text/plain
 Thu Jul 05 2012 20:05:15 GMT+0300 (EEST)




Content-Type: text/xml
<current-time>1341095876929</current-time>

REpresentational State Transfer              31
REST




Resource Representations

 • Data
      Thu Jul 05 2012 20:05:15 GMT+0300 (EEST)
 • Metadata
     Content-Type: text/plain
 • [Metadata to describe the
         metadata]


REpresentational State Transfer                 32
REST




Metadata

  • Name-Value Pairs
  • Resource Metadata
      Allow: GET, HEAD, OPTIONS

  • Representation Metadata
      Content-Type: text/plain




REpresentational State Transfer          33
REST




Control Data

• Defines purpose of the message
     GET

• Used to parameterize request
         (e.g. caching)
     If-Modified-Since: Sat, 29 Oct 1994
     19:43:31 GMT


REpresentational State Transfer                   34
REST




Media Types
 • Data Format = Media Type
 • Intention
   • Automated processing
   • Rendered / viewed by a user
   • Both
 • Composite Media Types
 • Affects Latency
REpresentational State Transfer           35
REST




Connectors
 • Client
 • Server
 • Cache
   • Shared (Akamai)
   • Non-shared (browser)
 • Resolver (DNS)
 • Tunnel (SSL)
REpresentational State Transfer          36
REST




Components

  •       Origin Server
  •       Gateway
  •       Proxy
  •       User Agent



REpresentational State Transfer          37
REST




                                   2
                                  Practice

REpresentational State Transfer                     38
REST




REpresentational State Transfer          39
REST




Booking Lifecycle
                                    update               cancel
                                                                   Cancelled


           create                  confirmed
                        Pending               Confirmed
                                                          "live"

                    rejected                                        Served

                                         update

                        Rejected                                    delete




REpresentational State Transfer                                                       40
REST




                                  CRUD


REpresentational State Transfer                 41
REST




    Create New Booking
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-20»,
     «to»: «2012-07-27»,
     «room-type»: «standard-2x»,
     «breakfast»: true
}




    REpresentational State Transfer                      42
REST




    Create New Booking
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-20»,
     «to»: «2012-07-27»,
     «room-type»: «standard-2x»,
     «breakfast»: true
}

                                      HTTP/1.1 201 Created
                                      Content-Type: application/json
                                      Location: http://www.booking-rest.com/bookings/9111

                                      {
                                          ...
                                          «state»: «PENDING»
                                      }


    REpresentational State Transfer                                                    42
REST




POST?




REpresentational State Transfer          43
REST




POST?

       • Server determines resource URI
       • We are changing server state
               (POST is not safe!)
       •       Resubmitting may create
               another booking (POST is non-
               idempotent!)

REpresentational State Transfer                   43
REST




    Creation Failure - 400
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     ...
     «hotel-id»: «mars:escapism»,
     ...
}




    REpresentational State Transfer          44
REST




    Creation Failure - 400
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     ...
     «hotel-id»: «mars:escapism»,
     ...
}




                                      HTTP/1.1 400 Bad Request
                                      Content-Type: application/json

                                      {
                                          «error»: «WTF is your hotel-id?»
                                      }




    REpresentational State Transfer                                        44
REST




    Creation Failure - 400
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     ...
     «hotel-id»: «mars:escapism»,
     ...
}




        Modify request before resubmitting!!!

    REpresentational State Transfer              45
REST




    Creation Failure - 500
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-20»,
     «to»: «2012-07-27»,
     «room-type»: «standard-2x»,
     «breakfast»: true
}



                                                  HTTP/1.1 500 Internal Server Error
                                                  Content-Type: application/json

                                                  {
                                                      «error»: «donno what went wrong»
                                                  }




    REpresentational State Transfer                                                    46
REST




    Creation Failure - 500
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-20»,
     «to»: «2012-07-27»,
     «room-type»: «standard-2x»,
     «breakfast»: true
}




                                        throw new Exception(«donno
                                             what went wrong»)

    REpresentational State Transfer                                 47
REST




Creation Failure - 500
        • Forward progress is tricky
                (booking created or not?)
                • Retry
                • Recompute state
        •       Try to be descriptive when
                returning 500 or avoid if it is
                possible
REpresentational State Transfer                          48
REST




    Creation Failure - 503
POST /bookings HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-20»,
     «to»: «2012-07-27»,
     «room-type»: «standard-2x»,
     «breakfast»: true
}

                                      HTTP/1.1 503 Service Unavailable
                                      Content-Type: application/json
                                      Retry-After: 60

                                      {
                                        «error»: «Have a headache right now. Please try
                                      again in a minute»
                                      }


    REpresentational State Transfer                                                    49
REST




                                  CRUD


REpresentational State Transfer                 50
REST




Reading Booking
GET /bookings/9111 HTTP/1.1
Host: www.booking-rest.com




                                  HTTP/1.1 200 OK
                                  Content-Type: application/json

                                  {
                                      «user-id»: «anonymous-developer»,
                                      «hotel-id»: «paris:bergere-opera-hotel-3»,
                                      «from»: «2012-07-20»,
                                      «to»: «2012-07-27»,
                                      «room-type»: «standard-2x»,
                                      «breakfast»: true
                                      «state»: «PENDING»
                                  }




REpresentational State Transfer                                                 51
REST




GET?

       • No side effects (GET is safe!)
       • Resubmitting will return the
               same result if resource haven’t
               changed (GET is idempotent /
               nullipotent!)



REpresentational State Transfer                     52
REST




Reading Booking (Later)
GET /bookings/9111 HTTP/1.1
Host: www.booking-rest.com




                                  HTTP/1.1 200 OK
                                  Content-Type: application/json

                                  {
                                      «user-id»: «anonymous-developer»,
                                      «hotel-id»: «paris:bergere-opera-hotel-3»,
                                      «from»: «2012-07-20»,
                                      «to»: «2012-07-27»,
                                      «room-type»: «standard-2x»,
                                      «breakfast»: true
                                      «state»: «SERVED»
                                  }




REpresentational State Transfer                                                53
REST




Reading Booking - 404


GET /bookings/mars HTTP/1.1
Host: www.booking-rest.com




                                  HTTP/1.1 404 Not Found




REpresentational State Transfer                                   54
REST




                                  CRUD


REpresentational State Transfer                 55
REST




    Updating Booking
PUT /bookings/9111 HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-22»,
     «to»: «2012-07-29»,
     «room-type»: «standard-2x»,
     «breakfast»: true,
     «state»: «PENDING»
}

                                                  HTTP/1.1 200 OK
                                                  Content-Type: application/json

                                                  {
                                                      ...
                                                      «state»: «PENDING»
                                                  }



    REpresentational State Transfer                                                   56
REST




PUT?




REpresentational State Transfer          57
REST




PUT?

       • Client determines resource URI
       • We are changing server state
               (PUT is not safe!)
       •       Resubmitting will result in the
               same resource state (PUT is
               idempotent!)

REpresentational State Transfer                         57
REST




    Updating Booking - 204
PUT /bookings/9111 HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-22»,
     «to»: «2012-07-29»,
     «room-type»: «standard-2x»,
     «breakfast»: true,
     «state»: «PENDING»
}




                                                  HTTP/1.1 204 No Content




    REpresentational State Transfer                                                58
REST




PUT: 200 or 204?


                • 200 more descriptive and
                        confirms server-side state
                •       204 is more efficient




REpresentational State Transfer                            59
REST




    Updating Booking - 409
PUT /bookings/9111 HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «user-id»: «anonymous-developer»,
     «hotel-id»: «paris:bergere-opera-hotel-3»,
     «from»: «2012-07-22»,
     «to»: «2012-07-29»,
     «room-type»: «standard-2x»,
     «breakfast»: true,
     «state»: «PENDING»
}

                                                  HTTP/1.1 409 Conflict
                                                  Content-Type: application/json

                                                  {
                                                      ...
                                                      «state»: «SERVED»
                                                  }



    REpresentational State Transfer                                                       60
REST




      PUT or
       POST
for Create/Update?

REpresentational State Transfer          61
REST




PUT/POST Guidelines
   • Use POST to create a resource identified by
            a service-generated URI
   •        Use POST to append a resource to (or to
            update existing resource in) a collection
            identified by a service-generated URI
   •        Use PUT to create or update a resource
            identified by a URI computed by the client
            sending full content of the specified
            resource

REpresentational State Transfer                                62
REST




                  Partial / Bulk
                    Updates?

REpresentational State Transfer           63
REST




Partial Update Strategies
1.     Encapsulate «partial» into its own sub-resource
       and invoke PUT on it
       • ... only if this new resource makes sense to
           others! Don’t do it just to avoid POST!
2.     Use POST for resource or sub-resource
       responding with «303 See Other» pointing to
       parent resource (Fielding разрешает!)
3.     Use PATCH (once universally deployed)


REpresentational State Transfer                           64
REST




    Partial Update - POST
POST /bookings/9111/dates HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
     «from»: «2012-07-22»,
     «to»: «2012-07-29»
}


                                      HTTP/1.1 303 See Other
                                      Location: http://www.booking-rest.com/bookings/9111


GET /bookings/9111 HTTP/1.1
Host: www.booking-rest.com


                                      HTTP/1.1 200 OK
                                      Content-Type: application/json

                                      ...



    REpresentational State Transfer                                                    65
REST




 Partial Update - PUT

PUT /bookings/9111/from HTTP/1.1
Host: www.booking-rest.com
Content-Type: text/plain

2012-07-22




                                   HTTP/1.1 200 OK
                                   Content-Type: text/plain

                                   2012-07-22




 REpresentational State Transfer                                     66
REST




Bulk Update Strategies




REpresentational State Transfer          67
REST




Bulk Update Strategies
1.     HTTP Pipelining
       1. Not well supported :(
       2. Innappropriate for non-idempotent methods
            like POST
2.     Well, use POST, as «universal» method. Respond
       with «303 See Other»
3.     If you need to remove a set of sub-resources
       and list is not very long, just issue DELETE
       listing ids in the URL

REpresentational State Transfer                         67
REST




Bulk Update - POST

POST /bookings/cancelled HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/json

{
    «ids»: [«9111», «9112», «9113», «9114»]
}




                                    HTTP/1.1 303 See Other
                                    Location: http://www.booking-rest.com/bookings




REpresentational State Transfer                                                      68
REST




                                  CRUD


REpresentational State Transfer                 69
REST




DELETE Booking - 204


DELETE /bookings/9111 HTTP/1.1
Host: www.booking-rest.com




                                  HTTP/1.1 204 No Content




REpresentational State Transfer                                    70
REST




 DELETE Booking - 200
DELETE /bookings/9111 HTTP/1.1
Host: www.booking-rest.com




                                   HTTP/1.1 200 OK
                                   Content-Type: application/json

                                   {
                                       «user-id»: «anonymous-developer»,
                                       «hotel-id»: «paris:bergere-opera-hotel-3»,
                                       «from»: «2012-07-22»,
                                       «to»: «2012-07-29»,
                                       «room-type»: «standard-2x»,
                                       «breakfast»: true,
                                       «state»: «SERVED»
                                   }




 REpresentational State Transfer                                                  71
REST




DELETE Booking - 404


DELETE /bookings/9111 HTTP/1.1
Host: www.booking-rest.com




                                  HTTP/1.1 404 Not Found




REpresentational State Transfer                                   72
REST




DELETE Disallowed - 405


DELETE /current-time HTTP/1.1
Host: www.time.com




                                  HTTP/1.1 405 Method Not Allowed
                                  Allow: GET




REpresentational State Transfer                                            73
REST




               Is it REST API?


REpresentational State Transfer          74
REST




               Is it REST API?
                      NO

REpresentational State Transfer          74
REST




                       Richardson
                        Maturity
                          Model

REpresentational State Transfer            75
REST




RMM




REpresentational State Transfer          76
REST




    Level 0 - Create
POST /bookingService HTTP/1.1
Content-Type: application/json

{
     «create»: {
       «hotel-id»: «paris:bergere-opera-hotel-3»,
       «from»: «2012-07-22»,
       «to»: «2012-07-29»,
       ...
     }
}
                                                HTTP/1.1 200 OK
                                                Content-Type: application/json

                                                {
                                                    «id»: «9111»
                                                }




    REpresentational State Transfer                                                     77
REST




    Level 0 - Retrieve
POST /bookingService HTTP/1.1
Content-Type: application/json

{
     «retrieve»: {
       «id»: «9111»
     }
}

                                      HTTP/1.1 200 OK
                                      Content-Type: application/json

                                      {
                                          «hotel-id»: «paris:bergere-opera-hotel-3»,
                                          «from»: «2012-07-22»,
                                          «to»: «2012-07-29»,
                                          ...
                                      }



    REpresentational State Transfer                                               78
REST




    Level 0 - Confirm
POST /bookingService HTTP/1.1
Content-Type: application/json

{
     «confirm»: {
       «id»: «9111»,
       «credit-card»: «1111-2222-3333-4444»,
       ...
     }
}

                                               HTTP/1.1 200 OK
                                               Content-Type: application/json

                                               {
                                                   «id»: «1234»
                                               }




    REpresentational State Transfer                                                    79
REST




Level 0




REpresentational State Transfer          80
REST




Level 0
1.     Single URI endpoint
2.     Single HTTP method
3.     Uses HTTP as transport, not app protocol
4.     Does not use mechanics of the Web
5.     Usually based on RPC




REpresentational State Transfer                          80
REST




Level 0
1.     Single URI endpoint
2.     Single HTTP method
3.     Uses HTTP as transport, not app protocol
4.     Does not use mechanics of the Web
5.     Usually based on RPC



             Flickr SOAP API,
            Google AdSense API
REpresentational State Transfer                          80
REST




    Level 1 - Create
POST /booking HTTP/1.1
Content-Type: application/json

{
     «create»: {
       «hotel-id»: «paris:bergere-opera-hotel-3»,
       «from»: «2012-07-22»,
       «to»: «2012-07-29»,
       ...
     }
}


                                                    HTTP/1.1 200 OK
                                                    Content-Type: application/json

                                                    {
                                                        «id»: «9111»
                                                    }




    REpresentational State Transfer                                                  81
REST




    Level 1 - Retrieve
POST /booking HTTP/1.1
Content-Type: application/json

{
     «retrieve»: {
       «id»: «9111»
     }
}

                                      HTTP/1.1 200 OK
                                      Content-Type: application/json

                                      {
                                          «hotel-id»: «paris:bergere-opera-hotel-3»,
                                          «from»: «2012-07-22»,
                                          «to»: «2012-07-29»,
                                          ...
                                      }



    REpresentational State Transfer                                               82
REST




    Level 1 - Confirm
POST /confirmation HTTP/1.1
Content-Type: application/json

{
     «create»: {
       «id»: «9111»,
       «credit-card»: «1111-2222-3333-4444»,
       ...
     }
}

                                               HTTP/1.1 200 OK
                                               Content-Type: application/json

                                               {
                                                   «id»: «1234»
                                               }




    REpresentational State Transfer                                                    83
REST




Level 1




REpresentational State Transfer          84
REST




Level 1
1. Multiple resources
2. Single HTTP method
3. Action in URI or payload




REpresentational State Transfer          84
REST




Level 1
1. Multiple resources
2. Single HTTP method
3. Action in URI or payload


               Flickr «REST» API,
               Amazon SimpleDB
REpresentational State Transfer            84
REST




Level 2




REpresentational State Transfer          85
REST




Level 2
1. Many URIs
2. Many verbs
3. We’ve designed it already ;)




REpresentational State Transfer          85
REST




Level 2
1. Many URIs
2. Many verbs
3. We’ve designed it already ;)

              Amazon S3
               Twitter API
           Google Calendar API
REpresentational State Transfer          85
REST




Level 2 APIs
                                     HTTP-based Type 1   HTTP-based Type 2

     Identification of Resources             Yes                  Yes


 Manipulation of Resources through
                                            Yes                  Yes
         Representations


     Self-Descriptive Messages               No                  Yes


             HATEOAS                         No                   No


             Examples                    Twitter API      Google Calendar API




REpresentational State Transfer                                                   86
REST




                                  Level 3?


REpresentational State Transfer                     87
REST




«If the engine of application state (and
  hence the API) is not being driven by
hypertext, then it cannot be RESTful and
     cannot be a REST API. Period.»


                                  Roy T. Fielding, 2008


REpresentational State Transfer                          88
REST




                            HATEOAS



REpresentational State Transfer              89
REST




Browsing




                           Follow links
                      Change application state
                      Move towards your goal

REpresentational State Transfer                         90
REST




Interaction
GET /bookings/9111 HTTP/1.1
Host: www.booking-rest.com

                                  HTTP/1.1 200 OK
                                  Content-Type: application/json

                                  {
                                      «user-id»: «anonymous-developer»,
                                      «hotel-id»: «paris:bergere-opera-hotel-3»,
                                      «from»: «2012-07-20»,
                                      «to»: «2012-07-27»,
                                      «room-type»: «standard-2x»,
                                      «breakfast»: true
                                      «state»: «PENDING»
                                  }




REpresentational State Transfer                                                       91
REST




Interaction
GET /bookings/9111 HTTP/1.1
Host: www.booking-rest.com

                                  HTTP/1.1 200 OK
                                  Content-Type: application/json

                                  {
                                      «user-id»: «anonymous-developer»,
                                      «hotel-id»: «paris:bergere-opera-hotel-3»,
                                      «from»: «2012-07-20»,
                                      «to»: «2012-07-27»,
                                      «room-type»: «standard-2x»,
                                      «breakfast»: true
                                      «state»: «PENDING»
                                  }


                 Exchange of resource state,
                    not application state
REpresentational State Transfer                                                       91
REST




Resource State
1.     Information belonging to the resource
2.     Links to related resources
3.     Possible transition(s) to a future state(s) of the
       resource

                                  {
                                      «user-id»: «anonymous-developer»,
                                      «hotel-id»: «paris:bergere-opera-hotel-3»,
                                      «from»: «2012-07-20»,
                                      «to»: «2012-07-27»,
                                      «room-type»: «standard-2x»,
                                      «breakfast»: true
                                      «state»: «PENDING»
                                  }



REpresentational State Transfer                                                       92
REST




Domain Application Protocol
    Legal interactions between consumer and a set
      of resources involved in a business process


                                                           update               cancel
                                                                                          Cancelled


                                  create                  confirmed
                                               Pending               Confirmed
                                                                                 "live"

                                           rejected                                        Served

                                                                update

                                               Rejected                                    delete




REpresentational State Transfer                                                                              93
REST




                    HTTP/1.1 200 OK
                    Content-Type: application/json

                    {
                        «user-id»: «anonymous-developer»,
                        «hotel-id»: «paris:bergere-opera-hotel-3»,
                        «from»: «2012-07-20»,
                        «to»: «2012-07-27»,
                        «room-type»: «standard-2x»,
                        «breakfast»: true
                        «state»: «PENDING»
                    }




REpresentational State Transfer                                             94
REST




                    HTTP/1.1 200 OK
                    Content-Type: application/json

                    {
                        «user-id»: «anonymous-developer»,
                        «hotel-id»: «paris:bergere-opera-hotel-3»,
                        «from»: «2012-07-20»,
                        «to»: «2012-07-27»,
                        «room-type»: «standard-2x»,
                        «breakfast»: true
                        «state»: «PENDING»
                    }




                             What’s next?

REpresentational State Transfer                                             94
REST




URI Templates

      - Service is bound to honoring
      these templates forever (and URIs
      do change!)
      - You’re exposing too much detail
      about implementation



REpresentational State Transfer              95
REST




                 Single entry-
                  point URI(s)

REpresentational State Transfer          96
REST




                 Single entry-
                  point URI(s)
                      http://www.booking-rest.com/bookings




REpresentational State Transfer                                     96
REST




Loose Coupling
Understanding of specific URI structure


‣ Change URI shapes
‣ Relocate resources to
different servers (partitioning)

REpresentational State Transfer           97
REST




Loose Coupling


       Semantics of a «link»!!!




REpresentational State Transfer          98
REST




Loose Coupling


       Semantics of a «link»!!!

                «link» = hypermedia control



REpresentational State Transfer                      98
REST




Hypermedia Control
           HTTP/1.1 200 OK
           Content-Type: application/json

           {
               «user-id»: «anonymous-developer»,
               «hotel-id»: «paris:bergere-opera-hotel-3»,
               «from»: «2012-07-20»,
               «to»: «2012-07-27»,
               «room-type»: «standard-2x»,
               «breakfast»: true,
               «state»: «PENDING»,
               «confirmation»: «http://www.booking-rest.com/confirmation/9111»
           }




REpresentational State Transfer                                                         99
REST




Hypermedia Control
          {
              ...
              «confirmation»: «http://www.booking-rest.com/confirmation/9111»,
              «cancellation»: «http://www.booking-rest.com/cancellation/9111»,
              ...
          }




REpresentational State Transfer                                                         100
REST




Hypermedia Control
          {
              ...
              «confirmation»: «http://www.booking-rest.com/confirmation/9111»,
              «cancellation»: «http://www.booking-rest.com/cancellation/9111»,
              ...
          }




  Elements have different protocol
semantics, but identical link semantics


REpresentational State Transfer                                                         100
REST




Better Hyperm Control
           HTTP/1.1 200 OK
           Content-Type: application/json

           {
               «user-id»: «anonymous-developer»,
               «hotel-id»: «paris:bergere-opera-hotel-3»,
               «from»: «2012-07-20»,
               «to»: «2012-07-27»,
               «room-type»: «standard-2x»,
               «breakfast»: true,
               «state»: «PENDING»,
               «link»: {
                 «rel»: «confirmation»
                 «href»: «http://www.booking-rest.com/confirmation/9111»
               }
           }




REpresentational State Transfer                                                   101
REST




Media Type
            HTTP/1.1 200 OK
            Content-Type: application/json

            {
                «user-id»: «anonymous-developer»,
                «hotel-id»: «paris:bergere-opera-hotel-3»,
                «from»: «2012-07-20»,
                «to»: «2012-07-27»,
                «room-type»: «standard-2x»,
                «breakfast»: true,
                «state»: «PENDING»,
                «link»: {
                  «rel»: «confirmation»,
                  «href»: «http://www.booking-rest.com/confirmation/9111»
                }
            }




REpresentational State Transfer                                                    102
REST




Media Type
            HTTP/1.1 200 OK
            Content-Type: application/json

            {
                «user-id»: «anonymous-developer»,
                «hotel-id»: «paris:bergere-opera-hotel-3»,
                «from»: «2012-07-20»,
                «to»: «2012-07-27»,
                «room-type»: «standard-2x»,
                «breakfast»: true,
                «state»: «PENDING»,
                «link»: {
                  «rel»: «confirmation»,
                  «href»: «http://www.booking-rest.com/confirmation/9111»
                }
            }


     Should it be interpreted as plain vanilla JSON?

REpresentational State Transfer                                                    102
REST




          Content-Type drives
           processing of the
               payload,
         NOT the payload itself

REpresentational State Transfer          103
REST




Media Type
            HTTP/1.1 200 OK
            Content-Type: application/json

            {
                «hotel-id»: «paris:bergere-opera-hotel-3»,
                ...
                «link»: {
                  «rel»: «confirmation»,
                  «href»: «http://www.booking-rest.com/confirmation/9111»
                }
            }



              Interpretative scheme for each format
             includes hypermedia control definitions



REpresentational State Transfer                                                    104
REST




Media Type
            HTTP/1.1 200 OK
            Content-Type: application/json

            {
                «hotel-id»: «paris:bergere-opera-hotel-3»,
                ...
                «link»: {
                  «rel»: «confirmation»,
                  «href»: «http://www.booking-rest.com/confirmation/9111»
                }
            }



              Interpretative scheme for each format
             includes hypermedia control definitions

                         JSON doesn’t define a «link»

REpresentational State Transfer                                                    104
REST




Better Media Type
            HTTP/1.1 200 OK
            Content-Type: application/vnd.booking+json

            {
                «user-id»: «anonymous-developer»,
                «hotel-id»: «paris:bergere-opera-hotel-3»,
                «from»: «2012-07-20»,
                «to»: «2012-07-27»,
                «room-type»: «standard-2x»,
                «breakfast»: true,
                «state»: «PENDING»,
                «link»: {
                  «rel»: «confirmation»
                  «href»: «http://www.booking-rest.com/confirmation/9111»
                }
            }




REpresentational State Transfer                                                    105
REST




                Should we give
              each representation
                 a media type?


REpresentational State Transfer            106
REST




Media Types /
Representations

‣ Usually there is NO 1:1 relationship between
media type and representation
‣ Usually having one single monolithic media type is
too bulky
‣ One media type per application domain context is
usually OK


REpresentational State Transfer                         107
REST




Create New Booking                HTTP/1.1 201 Created
                                  Content-Type: application/vnd.booking+json
                                  Location: http://www.booking-rest.com/bookings/9111

                                  {
                                      ...
                                      «links»: [
                                        {
                                           «rel»: «confirm»,
                                           «href»: «http://www.booking-rest.com/confirmation/9111»,
                                           «mediaType»: «application/vnd.booking+json»
                                        },
                                        {
                                           «rel»: «update»,
                                           «href»: «http://www.booking-rest.com/orders/9111»,
                                           «mediaType»: «application/vnd.booking+json»
                                        },
                                        {
                                           «rel»: «cancel»,
                                           «href»: «http://www.booking-rest.com/confirmation/9111»,
                                        },
                                        {
                                           «rel»: «self»,
                                           «href»: «http://www.booking-rest.com/orders/9111»,
                                           «mediaType»: «application/vnd.booking+json»
                                        }
                                      ]
                                      ...
                                  }


REpresentational State Transfer                                                                              108
REST




«link»

‣ URI - identifies a resource with which the
consumer can interact to progress the application
protocol
‣ rel - contains semantic markup (=> verb, headers,
structure of the payload)
‣ [mediaType] - format of the request payload



REpresentational State Transfer                       109
REST




    Confirm Booking
POST /confirmation/9111 HTTP/1.1
Host: www.booking-rest.com
Content-Type: application/vnd.booking+json

{
    «card-number»: «1111-2222-3333-4444»,
    «cvc»: «987»,
    «amount»: «1000.00»
}
                                             HTTP/1.1 200 OK
                                             Content-Type: application/vnd.booking+json
                                             Location: /confirmation/9111/1234

                                             {
                                                 ...
                                                 «links»: [
                                                   {
                                                      «rel»: «receipt»,
                                                      «href»: «http://www.booking-rest.com/confirmation/9111»
                                                   },
                                                   {
                                                      «rel»: «order»,
                                                      «href»: «http://www.booking-rest.com/orders/9111»,
                                                   }
                                                 ]
                                                 ...
                                             }


    REpresentational State Transfer                                                                     110
REST




 «REST doesn’t eliminate the need for a
clue. What REST does is concentrate that
   need for prior knowledge into readily
standardizable forms. That is the essential
  distinction between data-oriented and
       control-oriented integration.»

                                  Roy T. Fielding, 2008


REpresentational State Transfer                          111
REST




«... It has value because it is far easier to
standardize representation and relation
 types than it is to standardize objects
    and object-specific interfaces ...»


                                  Roy T. Fielding, 2008


REpresentational State Transfer                          112
REST




                        Versioning


REpresentational State Transfer             113
REST




1. Version in URI




REpresentational State Transfer          114
REST




1. Version in URI
                     http://www.booking-rest.com/v1/bookings




REpresentational State Transfer                                       114
REST




1. Version in URI
                     http://www.booking-rest.com/v1/bookings



    • /v1/bookings/9111 != /v2/bookings/9111?
        Not necessarily
    • Should client support both /v1 and /v2?
        Maintenance nightmare
    • Should client start constructing URIs then?
        Breaks HATEOAS


REpresentational State Transfer                                       114
REST




2. Version in Media Type




REpresentational State Transfer          115
REST




2. Version in Media Type


    • Another representation of /bookings/9111?
        Yes!
    • Should client support both /v1 and /v2?
        Client chooses which version to support
        through «Accept»
    • No need to construct URIs
        HATEOAS preserved

REpresentational State Transfer                          115
REST




2. Version in Media Type
                 application/vnd.booking.v2+json
                 application/vnd.booking+json; version=2.0



    • Another representation of /bookings/9111?
        Yes!
    • Should client support both /v1 and /v2?
        Client chooses which version to support
        through «Accept»
    • No need to construct URIs
        HATEOAS preserved

REpresentational State Transfer                                     115
REST




3. Version in Header




REpresentational State Transfer          116
REST




3. Version in Header

                                  X-REST-API-Version: 2.0




REpresentational State Transfer                                    116
REST




3. Version in Header

                                  X-REST-API-Version: 2.0




      Also fine but can be filtered out by
          proxies or intermediaries



REpresentational State Transfer                                    116
REST




                  Transactions


REpresentational State Transfer          117
REST




Transactions
1.     If you need transactions, treat them as
       corresponding resources
       1. Create transaction as POST
       2. Fill it with data and initiate by issuing PUT
2.     There is no concept of «distributed transaction»
       in REST. Treat «distributed» nature of the
       transaction as implementation detail




REpresentational State Transfer                           118
REST




Transaction
POST /transfer HTTP/1.1
Host: www.money-rest.com


                                  HTTP/1.1 201 Created
                                  Location: http://www.money-rest.com/transfer/9111


PUT /transfer/9111 HTTP/1.1
Host: www.money-rest.com
Content-Type: application/json

{
    «from»: «husband»,
    «to»: «wife»,
    «amount»: «1000.00»,
    «amount»: «UAH»,
    «description»: «fitness»
}

                                  HTTP/1.1 204 No Content



REpresentational State Transfer                                                       119
REST




                    Frameworks
                       (Java)

REpresentational State Transfer          120
REST




Thanks!
Questions?




                    121

Weitere ähnliche Inhalte

Was ist angesagt?

Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web servicesnbuddharaju
 
REST and RESTful Web Services
REST and RESTful Web ServicesREST and RESTful Web Services
REST and RESTful Web ServicesKasun Madusanke
 
The Rest Architectural Style
The Rest Architectural StyleThe Rest Architectural Style
The Rest Architectural StyleRobert Wilson
 
ReST (Representational State Transfer) Explained
ReST (Representational State Transfer) ExplainedReST (Representational State Transfer) Explained
ReST (Representational State Transfer) ExplainedDhananjay Nene
 
HATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from RESTHATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from RESTelliando dias
 
RESTful Architecture
RESTful ArchitectureRESTful Architecture
RESTful ArchitectureKabir Baidya
 
Restful web services by Sreeni Inturi
Restful web services by Sreeni InturiRestful web services by Sreeni Inturi
Restful web services by Sreeni InturiSreeni I
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webserviceDong Ngoc
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServicesPrateek Tandon
 
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRISThe glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRISGeert Pante
 
WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0Jeffrey West
 

Was ist angesagt? (20)

REST API Design
REST API DesignREST API Design
REST API Design
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Restful web services ppt
Restful web services pptRestful web services ppt
Restful web services ppt
 
REST and RESTful Web Services
REST and RESTful Web ServicesREST and RESTful Web Services
REST and RESTful Web Services
 
The Rest Architectural Style
The Rest Architectural StyleThe Rest Architectural Style
The Rest Architectural Style
 
ReST (Representational State Transfer) Explained
ReST (Representational State Transfer) ExplainedReST (Representational State Transfer) Explained
ReST (Representational State Transfer) Explained
 
HATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from RESTHATEOAS: The Confusing Bit from REST
HATEOAS: The Confusing Bit from REST
 
RESTful Architecture
RESTful ArchitectureRESTful Architecture
RESTful Architecture
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Restful web services by Sreeni Inturi
Restful web services by Sreeni InturiRestful web services by Sreeni Inturi
Restful web services by Sreeni Inturi
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webservice
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServices
 
126 Golconde
126 Golconde126 Golconde
126 Golconde
 
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRISThe glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
 
JSON and REST
JSON and RESTJSON and REST
JSON and REST
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0
 
Rest web services
Rest web servicesRest web services
Rest web services
 
Implementation advantages of rest
Implementation advantages of restImplementation advantages of rest
Implementation advantages of rest
 
Java web services
Java web servicesJava web services
Java web services
 

Ähnlich wie REpresentational State Transfer

Saturn 2018 rest.li
Saturn 2018 rest.liSaturn 2018 rest.li
Saturn 2018 rest.liMin Chen
 
ROC for Adaptive Systems
ROC for Adaptive SystemsROC for Adaptive Systems
ROC for Adaptive SystemsTom Mueck
 
Rest and beyond
Rest and beyondRest and beyond
Rest and beyondMing Yuan
 
-overview of res tful architecture-2
-overview of res tful architecture-2-overview of res tful architecture-2
-overview of res tful architecture-2JunHwan Huh
 
Introduction to REST
Introduction to RESTIntroduction to REST
Introduction to RESTkumar gaurav
 
RESTful Web Services @AnkaraPHP meetup
RESTful Web Services @AnkaraPHP meetupRESTful Web Services @AnkaraPHP meetup
RESTful Web Services @AnkaraPHP meetupFatih Karatana
 
2.1 Temporal RSCDF extension
2.1 Temporal RSCDF extension2.1 Temporal RSCDF extension
2.1 Temporal RSCDF extensionbutest
 
Semantic DESCription as a Service
Semantic DESCription as a ServiceSemantic DESCription as a Service
Semantic DESCription as a Serviceuji_geotec
 
Creating Restful Web Services with restish
Creating Restful Web Services with restishCreating Restful Web Services with restish
Creating Restful Web Services with restishGrig Gheorghiu
 
Toward data driven programming for RESTful Linked Data
Toward data driven programming for RESTful Linked DataToward data driven programming for RESTful Linked Data
Toward data driven programming for RESTful Linked DataSteffen Stadtmüller
 
The Characteristics of a RESTful Semantic Web and Why They Are Important
The Characteristics of a RESTful Semantic Web and Why They Are ImportantThe Characteristics of a RESTful Semantic Web and Why They Are Important
The Characteristics of a RESTful Semantic Web and Why They Are ImportantChimezie Ogbuji
 
SQL Data Service Overview
SQL Data Service OverviewSQL Data Service Overview
SQL Data Service OverviewEric Nelson
 
Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...
Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...
Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...Nathaniel Palmer
 
Introducing SQL Server Data Services
Introducing SQL Server Data ServicesIntroducing SQL Server Data Services
Introducing SQL Server Data Servicesgoodfriday
 
Introducing SQL Server Data Services
Introducing SQL Server Data ServicesIntroducing SQL Server Data Services
Introducing SQL Server Data Servicesgoodfriday
 
Introduction to WCF RIA Services for Silverlight 4 Developers
Introduction to WCF RIA Services for Silverlight 4 DevelopersIntroduction to WCF RIA Services for Silverlight 4 Developers
Introduction to WCF RIA Services for Silverlight 4 DevelopersSaltmarch Media
 

Ähnlich wie REpresentational State Transfer (19)

Saturn 2018 rest.li
Saturn 2018 rest.liSaturn 2018 rest.li
Saturn 2018 rest.li
 
ROC for Adaptive Systems
ROC for Adaptive SystemsROC for Adaptive Systems
ROC for Adaptive Systems
 
Rest and beyond
Rest and beyondRest and beyond
Rest and beyond
 
Lab7 paper
Lab7 paperLab7 paper
Lab7 paper
 
-overview of res tful architecture-2
-overview of res tful architecture-2-overview of res tful architecture-2
-overview of res tful architecture-2
 
Introduction to REST
Introduction to RESTIntroduction to REST
Introduction to REST
 
RESTful Web Services @AnkaraPHP meetup
RESTful Web Services @AnkaraPHP meetupRESTful Web Services @AnkaraPHP meetup
RESTful Web Services @AnkaraPHP meetup
 
2.1 Temporal RSCDF extension
2.1 Temporal RSCDF extension2.1 Temporal RSCDF extension
2.1 Temporal RSCDF extension
 
Semantic DESCription as a Service
Semantic DESCription as a ServiceSemantic DESCription as a Service
Semantic DESCription as a Service
 
Creating Restful Web Services with restish
Creating Restful Web Services with restishCreating Restful Web Services with restish
Creating Restful Web Services with restish
 
Toward data driven programming for RESTful Linked Data
Toward data driven programming for RESTful Linked DataToward data driven programming for RESTful Linked Data
Toward data driven programming for RESTful Linked Data
 
Rest surekha
Rest surekhaRest surekha
Rest surekha
 
The Characteristics of a RESTful Semantic Web and Why They Are Important
The Characteristics of a RESTful Semantic Web and Why They Are ImportantThe Characteristics of a RESTful Semantic Web and Why They Are Important
The Characteristics of a RESTful Semantic Web and Why They Are Important
 
SQL Data Service Overview
SQL Data Service OverviewSQL Data Service Overview
SQL Data Service Overview
 
Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...
Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...
Use of SOA and Web Services Technologies for EA Migration - Lessons Learned o...
 
Introducing SQL Server Data Services
Introducing SQL Server Data ServicesIntroducing SQL Server Data Services
Introducing SQL Server Data Services
 
Introducing SQL Server Data Services
Introducing SQL Server Data ServicesIntroducing SQL Server Data Services
Introducing SQL Server Data Services
 
About HTTP and REST
About HTTP and RESTAbout HTTP and REST
About HTTP and REST
 
Introduction to WCF RIA Services for Silverlight 4 Developers
Introduction to WCF RIA Services for Silverlight 4 DevelopersIntroduction to WCF RIA Services for Silverlight 4 Developers
Introduction to WCF RIA Services for Silverlight 4 Developers
 

Mehr von Vladimir Tsukur

GraphQL APIs in Scala with Sangria
GraphQL APIs in Scala with SangriaGraphQL APIs in Scala with Sangria
GraphQL APIs in Scala with SangriaVladimir Tsukur
 
GraphQL - APIs The New Way
GraphQL - APIs The New WayGraphQL - APIs The New Way
GraphQL - APIs The New WayVladimir Tsukur
 
Hypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix EngineeringHypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix EngineeringVladimir Tsukur
 
Hypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASHypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASVladimir Tsukur
 
Hot and spicy Java with Lombok. Live!
Hot and spicy Java with Lombok. Live!Hot and spicy Java with Lombok. Live!
Hot and spicy Java with Lombok. Live!Vladimir Tsukur
 
Building Awesome API with Spring
Building Awesome API with SpringBuilding Awesome API with Spring
Building Awesome API with SpringVladimir Tsukur
 
From CRUD to Hypermedia APIs with Spring
From CRUD to Hypermedia APIs with SpringFrom CRUD to Hypermedia APIs with Spring
From CRUD to Hypermedia APIs with SpringVladimir Tsukur
 
Together Cheerfully to Walk with Hypermedia
Together Cheerfully to Walk with HypermediaTogether Cheerfully to Walk with Hypermedia
Together Cheerfully to Walk with HypermediaVladimir Tsukur
 
Law of Demeter & Objective Sense of Style
Law of Demeter & Objective Sense of StyleLaw of Demeter & Objective Sense of Style
Law of Demeter & Objective Sense of StyleVladimir Tsukur
 
Abstraction Classes in Software Design
Abstraction Classes in Software DesignAbstraction Classes in Software Design
Abstraction Classes in Software DesignVladimir Tsukur
 
Acceptance Testing of Web UI
Acceptance Testing of Web UIAcceptance Testing of Web UI
Acceptance Testing of Web UIVladimir Tsukur
 

Mehr von Vladimir Tsukur (11)

GraphQL APIs in Scala with Sangria
GraphQL APIs in Scala with SangriaGraphQL APIs in Scala with Sangria
GraphQL APIs in Scala with Sangria
 
GraphQL - APIs The New Way
GraphQL - APIs The New WayGraphQL - APIs The New Way
GraphQL - APIs The New Way
 
Hypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix EngineeringHypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix Engineering
 
Hypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASHypermedia APIs and HATEOAS
Hypermedia APIs and HATEOAS
 
Hot and spicy Java with Lombok. Live!
Hot and spicy Java with Lombok. Live!Hot and spicy Java with Lombok. Live!
Hot and spicy Java with Lombok. Live!
 
Building Awesome API with Spring
Building Awesome API with SpringBuilding Awesome API with Spring
Building Awesome API with Spring
 
From CRUD to Hypermedia APIs with Spring
From CRUD to Hypermedia APIs with SpringFrom CRUD to Hypermedia APIs with Spring
From CRUD to Hypermedia APIs with Spring
 
Together Cheerfully to Walk with Hypermedia
Together Cheerfully to Walk with HypermediaTogether Cheerfully to Walk with Hypermedia
Together Cheerfully to Walk with Hypermedia
 
Law of Demeter & Objective Sense of Style
Law of Demeter & Objective Sense of StyleLaw of Demeter & Objective Sense of Style
Law of Demeter & Objective Sense of Style
 
Abstraction Classes in Software Design
Abstraction Classes in Software DesignAbstraction Classes in Software Design
Abstraction Classes in Software Design
 
Acceptance Testing of Web UI
Acceptance Testing of Web UIAcceptance Testing of Web UI
Acceptance Testing of Web UI
 

Kürzlich hochgeladen

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

REpresentational State Transfer

  • 1. REST REST Theory and Practice Vladimir Tsukur / Design Engineer & Team Lead @GlobalLogic, Partner @FrostDigital 2012
  • 5. REST «A software architecture is an abstraction of the run-time elements of a software system during some phase of its operation» Roy T. Fielding, 2000 REpresentational State Transfer 5
  • 6. REST Architecture Elements Components Components Components Data Connectors REpresentational State Transfer 6
  • 7. REST Architecture Elements SQL DAO DB REpresentational State Transfer 7
  • 8. REST «An architectural style is a coordinated set of architectural constraints that restricts the roles/features of architectural elements and the allowed relationships among those elements within any architecture that conforms to that style» Roy T. Fielding, 2000 REpresentational State Transfer 8
  • 9. REST REST = style REpresentational State Transfer 9
  • 10. REST It is actually a hybrid style ;) REpresentational State Transfer 10
  • 11. REST WWW Requirements First ‣ Low Entry-barrier ‣ Hypermedia ‣ Extensibility ‣ Prepare for change ‣ Distributed Hypermedia ‣ Large-grain data transfer ‣ Internet-scale ‣ Anarphic scalability ‣ Independent deployment REpresentational State Transfer 11
  • 12. REST REST Constraints REpresentational State Transfer 12
  • 13. REST 1. Client - Server + Separation of concerns + UI portability + Improved scalability REpresentational State Transfer 13
  • 14. REST 2. Stateless + Visibility - Performance + Reliability - Consistency + Scalability REpresentational State Transfer 14
  • 15. REST 3. Cache + Efficiency - Reliability + Scalability + UP Performance REpresentational State Transfer 15
  • 16. REST Client-Cache - Stateless-Server = < 1994 REpresentational State Transfer 16
  • 17. REST Web < 1994 - Static documents - CERN libwww common library - No consistent set of semantics for all resources REpresentational State Transfer 17
  • 18. REST 4. Uniform Interface + Simplicity - Efficiency + Visibility + Evolvability REpresentational State Transfer 18
  • 19. REST 5. Layered System + Simplicity - UP Performance + Scalability + Evolvability REpresentational State Transfer 19
  • 20. REST 6. [Code-on-Demand] + Client Simplicity - Visibility + Extensibility REpresentational State Transfer 20
  • 22. REST REST Data Elements REpresentational State Transfer 22
  • 24. REST Resources <a href=’ ’>Winnie</a> REpresentational State Transfer 23
  • 25. REST «A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.» Roy T. Fielding, 2000 REpresentational State Transfer 24
  • 26. REST Resources => {} => static: => dynamic: REpresentational State Transfer 25
  • 27. REST Resources M R(t)={ ids and/or representations } REpresentational State Transfer 26
  • 28. REST Resources 1.0 1.1 2.0 «latest» REpresentational State Transfer 27
  • 29. REST Resources 1.0 1.1 2.0 != «latest» REpresentational State Transfer 27
  • 30. REST Resources + Generality + Late binding of reference to implementation (request-time) + Allows to reference concept, not some singular representation REpresentational State Transfer 28
  • 33. REST Resource IDs «Since centralized link servers are an anathema to the immense scale and multi- organizational domain requirements of the Web, REST relies instead on the author choosing a resource identifier that best fits the nature of the concept being identified.» Roy T. Fielding, 2000 REpresentational State Transfer 30
  • 34. REST Resource Representations Content-Type: text/plain Thu Jul 05 2012 20:05:15 GMT+0300 (EEST) Content-Type: text/xml <current-time>1341095876929</current-time> REpresentational State Transfer 31
  • 35. REST Resource Representations • Data Thu Jul 05 2012 20:05:15 GMT+0300 (EEST) • Metadata Content-Type: text/plain • [Metadata to describe the metadata] REpresentational State Transfer 32
  • 36. REST Metadata • Name-Value Pairs • Resource Metadata Allow: GET, HEAD, OPTIONS • Representation Metadata Content-Type: text/plain REpresentational State Transfer 33
  • 37. REST Control Data • Defines purpose of the message GET • Used to parameterize request (e.g. caching) If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT REpresentational State Transfer 34
  • 38. REST Media Types • Data Format = Media Type • Intention • Automated processing • Rendered / viewed by a user • Both • Composite Media Types • Affects Latency REpresentational State Transfer 35
  • 39. REST Connectors • Client • Server • Cache • Shared (Akamai) • Non-shared (browser) • Resolver (DNS) • Tunnel (SSL) REpresentational State Transfer 36
  • 40. REST Components • Origin Server • Gateway • Proxy • User Agent REpresentational State Transfer 37
  • 41. REST 2 Practice REpresentational State Transfer 38
  • 43. REST Booking Lifecycle update cancel Cancelled create confirmed Pending Confirmed "live" rejected Served update Rejected delete REpresentational State Transfer 40
  • 44. REST CRUD REpresentational State Transfer 41
  • 45. REST Create New Booking POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true } REpresentational State Transfer 42
  • 46. REST Create New Booking POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true } HTTP/1.1 201 Created Content-Type: application/json Location: http://www.booking-rest.com/bookings/9111 { ... «state»: «PENDING» } REpresentational State Transfer 42
  • 48. REST POST? • Server determines resource URI • We are changing server state (POST is not safe!) • Resubmitting may create another booking (POST is non- idempotent!) REpresentational State Transfer 43
  • 49. REST Creation Failure - 400 POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { ... «hotel-id»: «mars:escapism», ... } REpresentational State Transfer 44
  • 50. REST Creation Failure - 400 POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { ... «hotel-id»: «mars:escapism», ... } HTTP/1.1 400 Bad Request Content-Type: application/json { «error»: «WTF is your hotel-id?» } REpresentational State Transfer 44
  • 51. REST Creation Failure - 400 POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { ... «hotel-id»: «mars:escapism», ... } Modify request before resubmitting!!! REpresentational State Transfer 45
  • 52. REST Creation Failure - 500 POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true } HTTP/1.1 500 Internal Server Error Content-Type: application/json { «error»: «donno what went wrong» } REpresentational State Transfer 46
  • 53. REST Creation Failure - 500 POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true } throw new Exception(«donno what went wrong») REpresentational State Transfer 47
  • 54. REST Creation Failure - 500 • Forward progress is tricky (booking created or not?) • Retry • Recompute state • Try to be descriptive when returning 500 or avoid if it is possible REpresentational State Transfer 48
  • 55. REST Creation Failure - 503 POST /bookings HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true } HTTP/1.1 503 Service Unavailable Content-Type: application/json Retry-After: 60 { «error»: «Have a headache right now. Please try again in a minute» } REpresentational State Transfer 49
  • 56. REST CRUD REpresentational State Transfer 50
  • 57. REST Reading Booking GET /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true «state»: «PENDING» } REpresentational State Transfer 51
  • 58. REST GET? • No side effects (GET is safe!) • Resubmitting will return the same result if resource haven’t changed (GET is idempotent / nullipotent!) REpresentational State Transfer 52
  • 59. REST Reading Booking (Later) GET /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true «state»: «SERVED» } REpresentational State Transfer 53
  • 60. REST Reading Booking - 404 GET /bookings/mars HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 404 Not Found REpresentational State Transfer 54
  • 61. REST CRUD REpresentational State Transfer 55
  • 62. REST Updating Booking PUT /bookings/9111 HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING» } HTTP/1.1 200 OK Content-Type: application/json { ... «state»: «PENDING» } REpresentational State Transfer 56
  • 64. REST PUT? • Client determines resource URI • We are changing server state (PUT is not safe!) • Resubmitting will result in the same resource state (PUT is idempotent!) REpresentational State Transfer 57
  • 65. REST Updating Booking - 204 PUT /bookings/9111 HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING» } HTTP/1.1 204 No Content REpresentational State Transfer 58
  • 66. REST PUT: 200 or 204? • 200 more descriptive and confirms server-side state • 204 is more efficient REpresentational State Transfer 59
  • 67. REST Updating Booking - 409 PUT /bookings/9111 HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING» } HTTP/1.1 409 Conflict Content-Type: application/json { ... «state»: «SERVED» } REpresentational State Transfer 60
  • 68. REST PUT or POST for Create/Update? REpresentational State Transfer 61
  • 69. REST PUT/POST Guidelines • Use POST to create a resource identified by a service-generated URI • Use POST to append a resource to (or to update existing resource in) a collection identified by a service-generated URI • Use PUT to create or update a resource identified by a URI computed by the client sending full content of the specified resource REpresentational State Transfer 62
  • 70. REST Partial / Bulk Updates? REpresentational State Transfer 63
  • 71. REST Partial Update Strategies 1. Encapsulate «partial» into its own sub-resource and invoke PUT on it • ... only if this new resource makes sense to others! Don’t do it just to avoid POST! 2. Use POST for resource or sub-resource responding with «303 See Other» pointing to parent resource (Fielding разрешает!) 3. Use PATCH (once universally deployed) REpresentational State Transfer 64
  • 72. REST Partial Update - POST POST /bookings/9111/dates HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «from»: «2012-07-22», «to»: «2012-07-29» } HTTP/1.1 303 See Other Location: http://www.booking-rest.com/bookings/9111 GET /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 200 OK Content-Type: application/json ... REpresentational State Transfer 65
  • 73. REST Partial Update - PUT PUT /bookings/9111/from HTTP/1.1 Host: www.booking-rest.com Content-Type: text/plain 2012-07-22 HTTP/1.1 200 OK Content-Type: text/plain 2012-07-22 REpresentational State Transfer 66
  • 75. REST Bulk Update Strategies 1. HTTP Pipelining 1. Not well supported :( 2. Innappropriate for non-idempotent methods like POST 2. Well, use POST, as «universal» method. Respond with «303 See Other» 3. If you need to remove a set of sub-resources and list is not very long, just issue DELETE listing ids in the URL REpresentational State Transfer 67
  • 76. REST Bulk Update - POST POST /bookings/cancelled HTTP/1.1 Host: www.booking-rest.com Content-Type: application/json { «ids»: [«9111», «9112», «9113», «9114»] } HTTP/1.1 303 See Other Location: http://www.booking-rest.com/bookings REpresentational State Transfer 68
  • 77. REST CRUD REpresentational State Transfer 69
  • 78. REST DELETE Booking - 204 DELETE /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 204 No Content REpresentational State Transfer 70
  • 79. REST DELETE Booking - 200 DELETE /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», «room-type»: «standard-2x», «breakfast»: true, «state»: «SERVED» } REpresentational State Transfer 71
  • 80. REST DELETE Booking - 404 DELETE /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 404 Not Found REpresentational State Transfer 72
  • 81. REST DELETE Disallowed - 405 DELETE /current-time HTTP/1.1 Host: www.time.com HTTP/1.1 405 Method Not Allowed Allow: GET REpresentational State Transfer 73
  • 82. REST Is it REST API? REpresentational State Transfer 74
  • 83. REST Is it REST API? NO REpresentational State Transfer 74
  • 84. REST Richardson Maturity Model REpresentational State Transfer 75
  • 86. REST Level 0 - Create POST /bookingService HTTP/1.1 Content-Type: application/json { «create»: { «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», ... } } HTTP/1.1 200 OK Content-Type: application/json { «id»: «9111» } REpresentational State Transfer 77
  • 87. REST Level 0 - Retrieve POST /bookingService HTTP/1.1 Content-Type: application/json { «retrieve»: { «id»: «9111» } } HTTP/1.1 200 OK Content-Type: application/json { «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», ... } REpresentational State Transfer 78
  • 88. REST Level 0 - Confirm POST /bookingService HTTP/1.1 Content-Type: application/json { «confirm»: { «id»: «9111», «credit-card»: «1111-2222-3333-4444», ... } } HTTP/1.1 200 OK Content-Type: application/json { «id»: «1234» } REpresentational State Transfer 79
  • 90. REST Level 0 1. Single URI endpoint 2. Single HTTP method 3. Uses HTTP as transport, not app protocol 4. Does not use mechanics of the Web 5. Usually based on RPC REpresentational State Transfer 80
  • 91. REST Level 0 1. Single URI endpoint 2. Single HTTP method 3. Uses HTTP as transport, not app protocol 4. Does not use mechanics of the Web 5. Usually based on RPC Flickr SOAP API, Google AdSense API REpresentational State Transfer 80
  • 92. REST Level 1 - Create POST /booking HTTP/1.1 Content-Type: application/json { «create»: { «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», ... } } HTTP/1.1 200 OK Content-Type: application/json { «id»: «9111» } REpresentational State Transfer 81
  • 93. REST Level 1 - Retrieve POST /booking HTTP/1.1 Content-Type: application/json { «retrieve»: { «id»: «9111» } } HTTP/1.1 200 OK Content-Type: application/json { «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-22», «to»: «2012-07-29», ... } REpresentational State Transfer 82
  • 94. REST Level 1 - Confirm POST /confirmation HTTP/1.1 Content-Type: application/json { «create»: { «id»: «9111», «credit-card»: «1111-2222-3333-4444», ... } } HTTP/1.1 200 OK Content-Type: application/json { «id»: «1234» } REpresentational State Transfer 83
  • 96. REST Level 1 1. Multiple resources 2. Single HTTP method 3. Action in URI or payload REpresentational State Transfer 84
  • 97. REST Level 1 1. Multiple resources 2. Single HTTP method 3. Action in URI or payload Flickr «REST» API, Amazon SimpleDB REpresentational State Transfer 84
  • 99. REST Level 2 1. Many URIs 2. Many verbs 3. We’ve designed it already ;) REpresentational State Transfer 85
  • 100. REST Level 2 1. Many URIs 2. Many verbs 3. We’ve designed it already ;) Amazon S3 Twitter API Google Calendar API REpresentational State Transfer 85
  • 101. REST Level 2 APIs HTTP-based Type 1 HTTP-based Type 2 Identification of Resources Yes Yes Manipulation of Resources through Yes Yes Representations Self-Descriptive Messages No Yes HATEOAS No No Examples Twitter API Google Calendar API REpresentational State Transfer 86
  • 102. REST Level 3? REpresentational State Transfer 87
  • 103. REST «If the engine of application state (and hence the API) is not being driven by hypertext, then it cannot be RESTful and cannot be a REST API. Period.» Roy T. Fielding, 2008 REpresentational State Transfer 88
  • 104. REST HATEOAS REpresentational State Transfer 89
  • 105. REST Browsing Follow links Change application state Move towards your goal REpresentational State Transfer 90
  • 106. REST Interaction GET /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true «state»: «PENDING» } REpresentational State Transfer 91
  • 107. REST Interaction GET /bookings/9111 HTTP/1.1 Host: www.booking-rest.com HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true «state»: «PENDING» } Exchange of resource state, not application state REpresentational State Transfer 91
  • 108. REST Resource State 1. Information belonging to the resource 2. Links to related resources 3. Possible transition(s) to a future state(s) of the resource { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true «state»: «PENDING» } REpresentational State Transfer 92
  • 109. REST Domain Application Protocol Legal interactions between consumer and a set of resources involved in a business process update cancel Cancelled create confirmed Pending Confirmed "live" rejected Served update Rejected delete REpresentational State Transfer 93
  • 110. REST HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true «state»: «PENDING» } REpresentational State Transfer 94
  • 111. REST HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true «state»: «PENDING» } What’s next? REpresentational State Transfer 94
  • 112. REST URI Templates - Service is bound to honoring these templates forever (and URIs do change!) - You’re exposing too much detail about implementation REpresentational State Transfer 95
  • 113. REST Single entry- point URI(s) REpresentational State Transfer 96
  • 114. REST Single entry- point URI(s) http://www.booking-rest.com/bookings REpresentational State Transfer 96
  • 115. REST Loose Coupling Understanding of specific URI structure ‣ Change URI shapes ‣ Relocate resources to different servers (partitioning) REpresentational State Transfer 97
  • 116. REST Loose Coupling Semantics of a «link»!!! REpresentational State Transfer 98
  • 117. REST Loose Coupling Semantics of a «link»!!! «link» = hypermedia control REpresentational State Transfer 98
  • 118. REST Hypermedia Control HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING», «confirmation»: «http://www.booking-rest.com/confirmation/9111» } REpresentational State Transfer 99
  • 119. REST Hypermedia Control { ... «confirmation»: «http://www.booking-rest.com/confirmation/9111», «cancellation»: «http://www.booking-rest.com/cancellation/9111», ... } REpresentational State Transfer 100
  • 120. REST Hypermedia Control { ... «confirmation»: «http://www.booking-rest.com/confirmation/9111», «cancellation»: «http://www.booking-rest.com/cancellation/9111», ... } Elements have different protocol semantics, but identical link semantics REpresentational State Transfer 100
  • 121. REST Better Hyperm Control HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING», «link»: { «rel»: «confirmation» «href»: «http://www.booking-rest.com/confirmation/9111» } } REpresentational State Transfer 101
  • 122. REST Media Type HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING», «link»: { «rel»: «confirmation», «href»: «http://www.booking-rest.com/confirmation/9111» } } REpresentational State Transfer 102
  • 123. REST Media Type HTTP/1.1 200 OK Content-Type: application/json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING», «link»: { «rel»: «confirmation», «href»: «http://www.booking-rest.com/confirmation/9111» } } Should it be interpreted as plain vanilla JSON? REpresentational State Transfer 102
  • 124. REST Content-Type drives processing of the payload, NOT the payload itself REpresentational State Transfer 103
  • 125. REST Media Type HTTP/1.1 200 OK Content-Type: application/json { «hotel-id»: «paris:bergere-opera-hotel-3», ... «link»: { «rel»: «confirmation», «href»: «http://www.booking-rest.com/confirmation/9111» } } Interpretative scheme for each format includes hypermedia control definitions REpresentational State Transfer 104
  • 126. REST Media Type HTTP/1.1 200 OK Content-Type: application/json { «hotel-id»: «paris:bergere-opera-hotel-3», ... «link»: { «rel»: «confirmation», «href»: «http://www.booking-rest.com/confirmation/9111» } } Interpretative scheme for each format includes hypermedia control definitions JSON doesn’t define a «link» REpresentational State Transfer 104
  • 127. REST Better Media Type HTTP/1.1 200 OK Content-Type: application/vnd.booking+json { «user-id»: «anonymous-developer», «hotel-id»: «paris:bergere-opera-hotel-3», «from»: «2012-07-20», «to»: «2012-07-27», «room-type»: «standard-2x», «breakfast»: true, «state»: «PENDING», «link»: { «rel»: «confirmation» «href»: «http://www.booking-rest.com/confirmation/9111» } } REpresentational State Transfer 105
  • 128. REST Should we give each representation a media type? REpresentational State Transfer 106
  • 129. REST Media Types / Representations ‣ Usually there is NO 1:1 relationship between media type and representation ‣ Usually having one single monolithic media type is too bulky ‣ One media type per application domain context is usually OK REpresentational State Transfer 107
  • 130. REST Create New Booking HTTP/1.1 201 Created Content-Type: application/vnd.booking+json Location: http://www.booking-rest.com/bookings/9111 { ... «links»: [ { «rel»: «confirm», «href»: «http://www.booking-rest.com/confirmation/9111», «mediaType»: «application/vnd.booking+json» }, { «rel»: «update», «href»: «http://www.booking-rest.com/orders/9111», «mediaType»: «application/vnd.booking+json» }, { «rel»: «cancel», «href»: «http://www.booking-rest.com/confirmation/9111», }, { «rel»: «self», «href»: «http://www.booking-rest.com/orders/9111», «mediaType»: «application/vnd.booking+json» } ] ... } REpresentational State Transfer 108
  • 131. REST «link» ‣ URI - identifies a resource with which the consumer can interact to progress the application protocol ‣ rel - contains semantic markup (=> verb, headers, structure of the payload) ‣ [mediaType] - format of the request payload REpresentational State Transfer 109
  • 132. REST Confirm Booking POST /confirmation/9111 HTTP/1.1 Host: www.booking-rest.com Content-Type: application/vnd.booking+json { «card-number»: «1111-2222-3333-4444», «cvc»: «987», «amount»: «1000.00» } HTTP/1.1 200 OK Content-Type: application/vnd.booking+json Location: /confirmation/9111/1234 { ... «links»: [ { «rel»: «receipt», «href»: «http://www.booking-rest.com/confirmation/9111» }, { «rel»: «order», «href»: «http://www.booking-rest.com/orders/9111», } ] ... } REpresentational State Transfer 110
  • 133. REST «REST doesn’t eliminate the need for a clue. What REST does is concentrate that need for prior knowledge into readily standardizable forms. That is the essential distinction between data-oriented and control-oriented integration.» Roy T. Fielding, 2008 REpresentational State Transfer 111
  • 134. REST «... It has value because it is far easier to standardize representation and relation types than it is to standardize objects and object-specific interfaces ...» Roy T. Fielding, 2008 REpresentational State Transfer 112
  • 135. REST Versioning REpresentational State Transfer 113
  • 136. REST 1. Version in URI REpresentational State Transfer 114
  • 137. REST 1. Version in URI http://www.booking-rest.com/v1/bookings REpresentational State Transfer 114
  • 138. REST 1. Version in URI http://www.booking-rest.com/v1/bookings • /v1/bookings/9111 != /v2/bookings/9111? Not necessarily • Should client support both /v1 and /v2? Maintenance nightmare • Should client start constructing URIs then? Breaks HATEOAS REpresentational State Transfer 114
  • 139. REST 2. Version in Media Type REpresentational State Transfer 115
  • 140. REST 2. Version in Media Type • Another representation of /bookings/9111? Yes! • Should client support both /v1 and /v2? Client chooses which version to support through «Accept» • No need to construct URIs HATEOAS preserved REpresentational State Transfer 115
  • 141. REST 2. Version in Media Type application/vnd.booking.v2+json application/vnd.booking+json; version=2.0 • Another representation of /bookings/9111? Yes! • Should client support both /v1 and /v2? Client chooses which version to support through «Accept» • No need to construct URIs HATEOAS preserved REpresentational State Transfer 115
  • 142. REST 3. Version in Header REpresentational State Transfer 116
  • 143. REST 3. Version in Header X-REST-API-Version: 2.0 REpresentational State Transfer 116
  • 144. REST 3. Version in Header X-REST-API-Version: 2.0 Also fine but can be filtered out by proxies or intermediaries REpresentational State Transfer 116
  • 145. REST Transactions REpresentational State Transfer 117
  • 146. REST Transactions 1. If you need transactions, treat them as corresponding resources 1. Create transaction as POST 2. Fill it with data and initiate by issuing PUT 2. There is no concept of «distributed transaction» in REST. Treat «distributed» nature of the transaction as implementation detail REpresentational State Transfer 118
  • 147. REST Transaction POST /transfer HTTP/1.1 Host: www.money-rest.com HTTP/1.1 201 Created Location: http://www.money-rest.com/transfer/9111 PUT /transfer/9111 HTTP/1.1 Host: www.money-rest.com Content-Type: application/json { «from»: «husband», «to»: «wife», «amount»: «1000.00», «amount»: «UAH», «description»: «fitness» } HTTP/1.1 204 No Content REpresentational State Transfer 119
  • 148. REST Frameworks (Java) REpresentational State Transfer 120

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n
  106. \n
  107. \n
  108. \n
  109. \n
  110. \n
  111. \n
  112. \n
  113. \n
  114. \n
  115. \n
  116. \n
  117. \n
  118. \n
  119. \n
  120. \n
  121. \n
  122. \n
  123. \n
  124. \n
  125. \n
  126. \n
  127. \n