Take a REST!

241
Vladimir Tsukur / Design Engineer & Team Lead @GlobalLogic, Partner @Easyhunt / Frost Digital 2014 Take a REST! Theory and Practice

description

Deep dive into REpresentational State Transfer as an architectural style for web. Clear definition of REST and RESTful API with clarification of common misconceptions. Overview of hypermedia & HATEOAS principles based on real examples. Sneak peak into HAL & other media types and lots of other useful stuff for engineers involved in developing and consuming HTTP APIs

Transcript of Take a REST!

  • 1. Vladimir Tsukur / Design Engineer & Team Lead @GlobalLogic, Partner @Easyhunt / Frost DigitalREST2014Take a REST!Theory and Practice

2. REpresentational State TransferREST2team &tech lead @vladimir tsukur partner @ 3. REpresentational State TransferREST3architectural elementsCCCooommmpppooonnneenentntsstsConnectorsData 4. REpresentational State TransferRESTGET /hist4cat http.log | grep GETGET /histPUT /doc/1cat http.log grep GETstdout stdinstdout 5. An architectural style is a set ofCONSTRAINTS that restricts the roles ofREpresentational State TransferREST5architectural elements Roy T. Fielding, 2000 6. REpresentational State TransferRESTRESTis a style forWEB6 7. REpresentationalREpresentational State TransferREST7StateTransfer 8. REpresentational State TransferREST8 9. REpresentational State TransferREST9 10. REpresentational State TransferREST10 11. REpresentational State TransferREST11REST ?style ? 12. REpresentational State TransferREST12Be of the web,not behind the webIan Robinson 13. REpresentational State TransferREST13WWW Requirements Low Entry-barrier Extensibility Distributed Hypermedia Internet-scaleHypermediaPrepare for changeLarge-grain data transferAnarchic scalabilityIndependent deployment 14. REpresentational State TransferREST14RESTConstraints 15. REpresentational State TransferREST151. Client - Server2. Stateless Server3. Cache4. Uniform Interface5. Layered System6. [Code-on-Demand] 16. REpresentational State TransferREST161. Client - Server 17. REpresentational State TransferREST171. Client - ServerPOST /hotels/lviv/nobilis/suite-room/booking!{from: 2014-09-10,to: 2012-09-20,breakfast: true}request 18. REpresentational State TransferREST171. Client - ServerPOST /hotels/lviv/nobilis/suite-room/booking!{from: 2014-09-10,to: 2012-09-20,breakfast: true}request201 Created!{id: 123,from: 2014-09-10,to: }response 19. REpresentational State TransferREST181. Client - ServerSeparation of Concerns:! Client responsible for UI Server responsible for data storage 20. REpresentational State TransferREST191. Client - Server 21. REpresentational State TransferREST191. Client - Server+ Scalability+ Simplicity+ Evolvability 22. REpresentational State TransferREST202. Stateless Server 23. REpresentational State TransferREST21Two Types of State Application State! Resource State 24. REpresentational State TransferREST22Application StateWhere you ARE in theinteraction / sessionPending ConfirmedServedcreateupdaterejectedcancel Cancelled"live"confirmedRejectedupdatedelete 25. REpresentational State TransferREST23Resource StateWhat is stored on the server(beyond session) 26. REpresentational State TransferREST242. Stateless Server Each request contains ALL theinformation necessary to understand it! Application (session) state is kept onthe client 27. REpresentational State TransferREST252. Stateless Server 28. REpresentational State TransferREST252. Stateless ServerGET /hotels?page request{hotels: ,_links: {next: /hotels?page=2}}response 29. GET /hotels?page=2 requestREpresentational State TransferREST252. Stateless ServerGET /hotels?page request{hotels: ,_links: {next: /hotels?page=2}}response{hotels: ,_links: {previous: /hotels?page=1,next: /hotels?page=3}}response 30. REpresentational State TransferREST262. Stateful Server 31. REpresentational State TransferREST262. Stateful ServerPOST /session requestresponse Set-Cookie: JSESSIONID=987 32. GET /hotelsCookie: JSESSIONID=987 requestREpresentational State TransferREST262. Stateful ServerPOST /session requestresponse Set-Cookie: JSESSIONID=987{hotels: } response 33. GET /hotelsCookie: JSESSIONID=987 requestGET /hotels/nextCookie: JSESSIONID=987 requestREpresentational State TransferREST262. Stateful ServerPOST /session requestresponse Set-Cookie: JSESSIONID=987{hotels: } response{hotels: } response 34. REpresentational State TransferREST27 35. REpresentational State TransferREST28Use Cookies forApplication State?aint RESTful, sorry! 36. REpresentational State TransferREST292. Stateless Server 37. REpresentational State TransferREST292. Stateless Server+ Visibility+ Reliability+ Scalability 38. REpresentational State TransferREST292. Stateless Server+ Visibility+ Reliability+ Scalability- Performance- Consistency 39. REpresentational State TransferREST303. Cache 40. REpresentational State TransferREST313. Cache Acts as a mediator between client &server potentially reusing responses toequivalent requests 41. REpresentational State TransferREST323. Cache 42. REpresentational State TransferREST323. CacheGET /hotels request200 OKCache-Control: max-age=3600, must-revalidate!{hotels: ,}response 43. REpresentational State TransferREST323. CacheGET /hotels request200 OKCache-Control: max-age=3600, must-revalidate!{hotels: ,}responserequestcacheGET /hotels200 OKCache-Control: max-age=3600, must-revalidate!{hotels: ,} 44. REpresentational State TransferREST333. Cache 45. REpresentational State TransferREST333. Cache+ Efficiency+ Scalability+ UP Performance 46. REpresentational State TransferREST333. Cache+ Efficiency+ Scalability+ UP Performance- Reliability 47. REpresentational State TransferREST34Client-Cache -Stateless-Server =Web < 1994 48. REpresentational State TransferREST35Web < 1994- Static documents- CERN libwww common library- No consistent set of semanticsfor all resources 49. REpresentational State TransferREST36 50. REpresentational State TransferREST374. Uniform Interface 51. REpresentational State TransferREST384. Uniform Interface1. Identification of resources2. Manipulation of resources throughrepresentations3. Self-descriptive messages4. HATEOAS 52. 4. Uniform InterfaceREpresentational State TransferREST39Resourceis a key abstraction 53. REpresentational State TransferREST40/images/beautiful-lviv.jpg 54. REpresentational State TransferREST41/images/beautiful-kyiv.jpg 55. REpresentational State TransferREST42/documents/folder/profile.doc 56. REpresentational State TransferREST43/time/current 57. REpresentational State TransferREST44/api/hotels/lviv 58. REpresentational State TransferREST45/api/hotels/lviv/nobilis 59. REpresentational State TransferREST46/api/hotels/lviv/nobilis/classic 60. REpresentational State TransferREST47/abstraction 61. 4. Uniform InterfaceREpresentational State TransferREST48resource => entity!entity > resource 62. 4. Uniform InterfaceResourcesREpresentational State TransferREST49/v1 63. 4. Uniform InterfaceResourcesREpresentational State TransferREST49/v1 /v2 64. REpresentational State TransferREST49/v1/latest/v24. Uniform InterfaceResources 65. REpresentational State TransferREST49/v1/v2 /v3/latest4. Uniform InterfaceResources 66. 4. Uniform InterfaceResourcesREpresentational State TransferREST49/v1/v2 /v3/latest/v2 != /latest/v3 != /latest 67. 4. Uniform InterfaceResourcesREpresentational State TransferREST50=> {}=> static:!=> dynamic: 68. 4. Uniform InterfaceResourcesREpresentational State TransferREST51+ Generality+ Allows late binding+ Allows to referenceconcept, not singularrepresentation 69. REpresentational State TransferREST524. Uniform Interface4.1. Identification of Resources 70. 4. Uniform Interface4.1. Identification of ResourcesREpresentational State TransferREST53http://www.google.comftp://user:password@host:port/pathurn:ISBN:0-395-36341-1 71. REpresentational State TransferREST544. Uniform Interface4.2. Manipulation of Resourcesthrough Representations 72. 4. Uniform InterfaceContent Negotiation = ConnegREpresentational State TransferREST55GET /current-timeAccept: text/plain request 73. 4. Uniform InterfaceContent Negotiation = ConnegREpresentational State TransferREST55GET /current-timeAccept: text/plain request200 OKContent-Type: text/plain!Thu Aug 30 2014 09:30:15 GMT+0300 (EEST)response 74. 4. Uniform InterfaceConnegREpresentational State TransferREST56GET /current-timeAccept: text/xml request 75. 4. Uniform InterfaceConnegREpresentational State TransferREST56GET /current-timeAccept: text/xml request200 OKContent-Type: text/xml!1341095876929response 76. REpresentational State TransferREST57GET /current-timeAccept: text/plain;q=0.8,text/xml;q=0.9request4. Uniform InterfaceConneg 77. 4. Uniform InterfaceConnegREpresentational State TransferREST57GET /current-timeAccept: text/plain;q=0.8,text/xml;q=0.9request200 OKContent-Type: text/xml!1341095876929response 78. REpresentational State TransferREST584. Uniform InterfaceRepresentation Structure200 OKContent-Type: text/plainContent-Length: 41!Thu Aug 30 2014 09:30:15 GMT+0300 (EEST) 79. MetadataREpresentational State TransferREST584. Uniform InterfaceRepresentation Structure200 OKContent-Type: text/plainContent-Length: 41!Thu Aug 30 2014 09:30:15 GMT+0300 (EEST) 80. MetadataREpresentational State TransferREST584. Uniform InterfaceRepresentation Structure200 OKContent-Type: text/plainContent-Length: 41!Thu Aug 30 2014 09:30:15 GMT+0300 (EEST)Data 81. 4. Uniform InterfaceREpresentational State TransferREST59Content-Type drivesprocessing of the payload,NOT the payload itself 82. = Data Format Intention: Automated processing (JSON, XML) Rendered / viewed by a user (HTML) Both Composite Media Types Affects LatencyREpresentational State TransferREST604. Uniform InterfaceMedia Types (Content-Type) 83. 4. Uniform InterfaceControl Data Defines purpose of the messageGETREpresentational State TransferREST61 Used to parameterize request(e.g. caching)If-Modified-Since: Sat, 29 Oct 199419:43:31 GMT 84. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheable 85. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET 86. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET 87. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET 88. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET 89. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST 90. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST 91. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST 92. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST 93. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT 94. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT 95. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT 96. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT 97. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH 98. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH 99. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH 100. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH 101. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE 102. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE 103. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE 104. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE 105. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE HEAD 106. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE HEAD 107. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE HEAD 108. REpresentational State TransferREST624. Uniform Interfacesafe idempotent cacheableGET POST PUT PATCH DELETE HEAD 109. REpresentational State TransferREST634. Uniform InterfaceGET,PUT,POST,DELETE!CRUD 110. REpresentational State TransferREST644. Uniform InterfaceREST CRUD CRUD represent operations on data CRUD pushes business logic to caller 111. 4. Uniform InterfaceREpresentational State TransferREST65PUT orPOSTfor Create/Update? 112. 4. Uniform InterfaceREpresentational State TransferREST66PUT/POST Guidelines Use POST to create a resource identified bya service-generated URI Use POST to append a resource to (or toupdate existing resource in) a collectionidentified by a service-generated URI Use PUT to create or update a resourceidentified by a URI received by the clientsending full content of the specifiedresource 113. 4. Uniform InterfaceREpresentational State TransferREST67POSTPOST /bookings{room-id: lviv:nobilis:suite,data: {from: 2014-09-10,to: 2012-09-20,breakfast: true}}request201 CreatedLocation: /bookings/1{id: 123,room-id: lviv:nobilis:suite,data: }response 114. 4. Uniform InterfaceREpresentational State TransferREST67POSTPOST /bookings{room-id: lviv:nobilis:suite,data: {from: 2014-09-10,to: 2012-09-20,breakfast: true}}request201 CreatedLocation: /bookings/1{id: 123,room-id: lviv:nobilis:suite,data: }response 115. 4. Uniform InterfaceREpresentational State TransferREST67POSTPOST /bookings{room-id: lviv:nobilis:suite,data: {from: 2014-09-10,to: 2012-09-20,breakfast: true}}request201 CreatedLocation: /bookings/1{id: 123,room-id: lviv:nobilis:suite,data: }response 116. 4. Uniform InterfaceREpresentational State TransferREST68PUTPUT /user/mykola/address{street: M. Grinchenko,house: 2/1}request200 OK / 204 No Content{street: M. Grinchenko,house: 2/1}response 117. 4. Uniform Interface4.3. Self-descriptive messagesREpresentational State TransferREST69Each message includes enoughinformation to describe how to processthe message200 OKContent-Type: application/jsonCache-Control: max-age=3600, must-revalidate!{hotels: ,} 118. 4. Uniform Interface4.4. HATEOASREpresentational State TransferREST70Application state transitions fullydriven by hypermedia 119. REpresentational State TransferREST714. Uniform Interface 120. REpresentational State TransferREST714. Uniform Interface+ Simplicity+ Visibility+ Evolvability 121. REpresentational State TransferREST714. Uniform Interface+ Simplicity+ Visibility+ Evolvability- Efficiency 122. REpresentational State TransferREST725. Layered System 123. REpresentational State TransferREST725. Layered System 124. 5. Layered SystemREpresentational State TransferREST73Each component does not seebeyond the immediate layer!Intermediaries: Proxies Gateways 125. REpresentational State TransferREST745. Layered System 126. REpresentational State TransferREST745. Layered System+ Simplicity+ Scalability+ Evolvability 127. REpresentational State TransferREST745. Layered System+ Simplicity+ Scalability+ Evolvability- UP Performance 128. REpresentational State TransferREST756. [Code-on-Demand] 129. REpresentational State TransferREST766. [Code-on-Demand] 130. REpresentational State TransferREST776. [Code-on-Demand] 131. REpresentational State TransferREST786. [Code-on-Demand] 132. REpresentational State TransferREST786. [Code-on-Demand]+ Client Simplicity+ Extensibility 133. REpresentational State TransferREST786. [Code-on-Demand]+ Client Simplicity+ Extensibility - Visibility 134. REpresentational State TransferREST796. [Code-on-Demand]vs 135. REpresentational State TransferREST796. [Code-on-Demand]vs+ Simplicity+ Visibility+ UP Performance 136. REpresentational State TransferREST80REST 137. REpresentational State TransferREST81profit+ Performance+ Scalability+ Simplicity+ Evolvability+ Visibility+ Portability 138. REpresentational State TransferREST82Default REST Impl HTTP (RFC 2616,RFC 7230-7235) URI (RFC 3986) HTML 139. REpresentational State TransferREST83RichardsonMaturityModel 140. REpresentational State TransferREST84RMM 141. REpresentational State TransferREST85Level 0 - BookPOST /bookings{createBooking: {room-id: lviv:nobilis:suite,data: {from: 2014-09-10,to: 2012-09-20,breakfast: true}}}request200 OK{success: {id: 123,room-id: lviv:nobilis:suite,data: }}response 142. REpresentational State TransferREST86Level 0 - Get BookingPOST /bookings{getBooking: {id: 123}}request200 OK{success: {id: 123,room-id: lviv:nobilis:suite,data: }}response 143. REpresentational State TransferREST87Level 01. Single URI endpoint2. Single HTTP method3. Uses HTTP as transport, not app protocol4. Does not use mechanics of the Web5. Usually based on RPCFlickr SOAP API,Google AdSense API 144. REpresentational State TransferREST88Level 1 - BookPOST /hotel/lviv/nobilis/suite{createBooking: {from: 2014-09-10,to: 2012-09-20,breakfast: true}}request200 OK{success: {id: 123,room-id: lviv:nobilis:suite,data: }}response 145. REpresentational State TransferREST89Level 1 - Get BookingPOST /bookings/123{getBooking: {id: 123}}request200 OK{success: {id: 123,room-id: lviv:nobilis:suite,data: }}response 146. REpresentational State TransferREST90Level 11. Multiple resources2. Single HTTP method3. Action in URI or payloadFlickr REST API,Amazon SimpleDB 147. REpresentational State TransferREST91Level 2 - BookPOST /hotel/lviv/nobilis/suite/booking{from: 2014-09-10,to: 2012-09-20,breakfast: true}request200 OK{id: 123,room-id: lviv:nobilis:suite,data: }response 148. REpresentational State TransferREST92Level 2 - Get BookingGET /bookings/123request200 OK{id: 123,room-id: lviv:nobilis:suite,data: }response 149. REpresentational State TransferREST93Level 21. Many URIs2. Many verbs3. But NO hypermediaAmazon S3Twitter APIGoogle Calendar API 150. REpresentational State TransferREST94Level 2 APIsHTTP-based Type 1 HTTP-based Type 2Identification of Resources Yes YesManipulation of Resources throughRepresentations Yes YesSelf-Descriptive Messages No YesHATEOAS No NoExamples Twitter API Google Calendar API 151. REpresentational State TransferREST95Level 3? 152. REpresentational State TransferREST962Hypermedia &Media Types 153. REpresentational State TransferREST97 154. If the engine of application state (andhence the API) is not being driven byhypertext, then it cannot be RESTful andREpresentational State TransferREST98cannot be a REST API. Period.Roy T. Fielding, 2008 155. REpresentational State TransferREST99HATEOAS 156. REpresentational State TransferREST100BrowsingFollow linksChange application stateMove towards your goal 157. REpresentational State TransferREST101Take a RESThttps://github.com/flushdia/take-a-REST 158. {take-a-rest:hotel: {href: http://localhost:8080/api/hotels/2,mediaType: application/hal+json}}REpresentational State TransferREST102link URI - identifies a resource with which theconsumer can interact to progress the applicationprotocol rel - contains semantic markup (=> verb, headers,structure of the payload) mediaType - format of the payload 159. REpresentational State TransferREST103Legal interactions between consumer and a setof resources involved in a business processPending ConfirmedServedcreateupdaterejectedcancel Cancelled"live"confirmedRejectedupdatedeleteDomain ApplicationProtocol 160. REpresentational State TransferREST104Application Lifecycle 161. REpresentational State TransferREST105Booking Lifecycle 162. REpresentational State TransferREST106 163. REpresentational State TransferREST107CREATED BookingInitial Transitions 164. REpresentational State TransferREST108CREATED Bookingpayment N/A - to be paid on the spotN/A 165. if (booking.links.has(payment)) {// draw payment button / UI}REpresentational State TransferREST109Client 166. REpresentational State TransferREST110HATEOAS clientdoes NOT break,because it does NOTexpect link to bealways available 167. REpresentational State TransferREST111CREATED Bookingservice link added - new functionalityN/A 168. REpresentational State TransferREST112Upgraded / new clientMAY leverage newfeatures when updated.!Existing clients stay intact 169. REpresentational State TransferREST113Client may knowHOW,butNOT WHEN 170. REST doesnt eliminate the need for aclue. What REST does is concentrate thatneed for prior knowledge into readilystandardizable forms. That is the essentialdistinction between data-oriented andREpresentational State TransferREST114control-oriented integration.Roy T. Fielding, 2008 171. ... It has value because it is far easier tostandardize representation and relationtypes than it is to standardize objectsand object-specific interfaces ...REpresentational State TransferRESTRoy T. Fielding, 2008115 172. REpresentational State TransferREST116profit API: explorable & self-documented Client: No URL construction No domain logic replication Less coupling Server: Transparent resource relocation Easier versioning & evolvability 173. REpresentational State TransferREST117Is somebodyhypermedia-ing? 174. REpresentational State TransferREST118 175. URI Template ContractREpresentational State TransferREST119URL Methods/api/bookings GET, POST/api/bookings/{id} GET, POST, PUT, DELETE/api/hotels GET/api/hotels/{id} GET 176. URI Template ContractNOT HATEOASREpresentational State TransferREST119URL Methods/api/bookings GET, POST/api/bookings/{id} GET, POST, PUT, DELETE/api/hotels GET/api/hotels/{id} GET 177. REpresentational State TransferREST120URI Template Contract- Client is bound to honoring URIstructure (URIs change!)- Exposing more detail aboutimplementation then necessary- Domain-specific logic duplicated onthe client-side 178. REpresentational State TransferREST121RESTdoes NOT careabout URI values/whatever/is/fine/to/be/restful 179. REpresentational State TransferREST122Single entry-pointURI(s) */api* (or a limited set of URIs) 180. REpresentational State TransferREST123Other stuffdiscovered vialinks 181. REpresentational State TransferREST124Extras! 182. REpresentational State TransferREST125Resource State 183. REpresentational State TransferREST126Resource State1. Information belonging to the resource2. Links to related resources3. Possible transition(s) to a futurestate(s) of the resource 184. REpresentational State TransferREST127Resource State1. Info belonging to resource 185. REpresentational State TransferREST128Resource State2. Links to related resources 186. REpresentational State TransferREST129Resource State3. Possible state transitions 187. 4. Uniform InterfaceREpresentational State TransferREST130Media TypeHTTP/1.1 200 OKContent-Type: application/json!{_links: ,_embedded: ,city: "Kyiv",from: [ 2014, 10, 1 ],to: [ 2014, 10, 15 ],hotel: "Premier Palace",includeBreakfast: false,paid: true,price: 1000,roomType: "ROOM"} 188. 4. Uniform InterfaceHTTP/1.1 200 OKContent-Type: application/json!{_links: ,_embedded: ,city: "Kyiv",from: [ 2014, 10, 1 ],to: [ 2014, 10, 15 ],hotel: "Premier Palace",includeBreakfast: false,paid: true,price: 1000,roomType: "ROOM"}REpresentational State TransferREST130Media TypeShould it be interpreted as plain vanilla JSON? 189. 4. Uniform InterfaceREpresentational State TransferREST131Better Media TypeHTTP/1.1 200 OKContent-Type: application/vnd.take-a-rest.booking+json!{_links: ,_embedded: ,city: "Kyiv",from: [ 2014, 10, 1 ],to: [ 2014, 10, 15 ],hotel: "Premier Palace",includeBreakfast: false,paid: true,price: 1000,roomType: "ROOM"} 190. 4. Uniform InterfaceREpresentational State TransferREST132Media Type ProfileHTTP/1.1 200 OKContent-Type: application/hal+json; profile=take-a-rest:booking!{_links: ,_embedded: ,city: "Kyiv",from: [ 2014, 10, 1 ],to: [ 2014, 10, 15 ],hotel: "Premier Palace",includeBreakfast: false,paid: true,price: 1000,roomType: "ROOM"} 191. 4. Uniform InterfaceREpresentational State TransferREST133Should we giveeach representationa media type? 192. 4. Uniform InterfaceREpresentational State TransferREST134Media Types /Representations Usually there is NO 1:1 relationship between mediatype and representation Usually having one single monolithic media type istoo bulky One media type per application domain context isusually OK 193. A REST API should spend almost all of itsdescriptive effort in defining the mediatype(s) used for representing resources anddriving application state, or in definingREpresentational State TransferREST135extended relation names and/orhypertext-enabled mark-up for existingstandard media types.Roy T. Fielding, 2008 194. REpresentational State TransferREST136Hypermedia FactorsMeasurement of the levelof hypermedia support 195. Hypermedia Factors / Link SupportREpresentational State TransferREST137LE = Link EmbeddedHTTP GET 196. Hypermedia Factors / Link SupportREpresentational State TransferREST138LO = Link OutboundNobilis LvivHTTP GET 197. Hypermedia Factors / Link SupportREpresentational State TransferREST139LT = Templated QueriesHTTP GET 198. LN = Non-Idempotent UpdatesREpresentational State TransferREST140Hypermedia Factors / Link SupportHTTP POST 199. Hypermedia Factors / Link SupportREpresentational State TransferREST141LI = Idempotent Updatesnew XmlHttpRequest().open("DELETE", "/bookings/3")HTTP PUT, DELETE 200. Hypermedia Factors / Control Data SupportREpresentational State TransferREST142CR = Read ModificationHTTP Accept-* headers 201. Hypermedia Factors / Control Data SupportREpresentational State TransferREST143CU = Update ModificationHTTP Content-* headers 202. Hypermedia Factors / Control Data SupportREpresentational State TransferREST144CM = Method ModificationHTTP GET/PUT/POST/ 203. Hypermedia Factors / Control Data SupportREpresentational State TransferREST145CL = Link SemanticsHTTP GET/PUT/POST/ 204. REpresentational State TransferREST146Hypermedia FactorsWhat about and ? 205. REpresentational State TransferRESTHTML XML JSONLE LO LT LN LI CR CU CM CL 147Hypermedia Factors 206. REpresentational State TransferRESTJSON-LD JSON API HAL Cj Siren Mason UberLE LO LT LN LI CR CU CM CL 148JSON-based Media Types 207. REpresentational State TransferREST149HAL OverviewHypertext Application Language: simple format explorable & discoverable APIs for JSON:! application/vnd+json for XML:application/vnd+xml 208. REpresentational State TransferREST150HAL 209. REpresentational State TransferREST151HAL - state 210. REpresentational State TransferREST152HAL - links 211. HAL - embedded resourcesREpresentational State TransferREST153 212. REpresentational State TransferREST154HAL 213. REpresentational State TransferREST155HAL - CURies 214. REpresentational State TransferREST155HAL - CURies Resource documentation Link name-spacing 215. REpresentational State TransferREST156HAL Language Support Java Spring HATEOAS halbuilder-java halarious HyperExpress-HAL JavaScript Scala, Clojure, Ruby, Python, PHP, ObjectiveC, C#, Eiffel, Go, Rust, 216. REpresentational State TransferREST157HAL APIs 217. REpresentational State TransferREST158Versioning 218. REpresentational State TransferREST1591. Version in URI 219. REpresentational State TransferREST1591. Version in URIhttp://www.booking-rest.com/v1/bookings 220. http://www.booking-rest.com/v1/bookingsREpresentational State TransferREST1591. Version in URI /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 221. REpresentational State TransferREST1602. Version in Media Type 222. REpresentational State TransferREST1602. Version in Media Type Another representation of /bookings/9111?Yes! Should client support both /v1 and /v2?Client chooses which version to supportthrough Accept No need to construct URIsHATEOAS preserved 223. application/vnd.booking.v2+jsonapplication/vnd.booking+json; version=2.0REpresentational State TransferREST1602. Version in Media Type Another representation of /bookings/9111?Yes! Should client support both /v1 and /v2?Client chooses which version to supportthrough Accept No need to construct URIsHATEOAS preserved 224. REpresentational State TransferREST1613. Version in Header 225. REpresentational State TransferREST1613. Version in HeaderX-REST-API-Version: 2.0 226. REpresentational State TransferREST1613. Version in HeaderX-REST-API-Version: 2.0Can be filtered out by proxies orintermediaries 227. REpresentational State TransferREST1624. Best AdviceDO NOT VERSION! 228. REpresentational State TransferREST1624. Best AdviceDO NOT VERSION!Support backwards compatibility,enable discovery 229. REpresentational State TransferREST163API SurveyMarch 2014180+ respondents 230. Security Usability Can't decideREpresentational State TransferREST164API Survey - Top Priority18 %38 %44 % 231. JSON XML OtherREpresentational State TransferREST165API Survey - Format2 %48 % 51 % 232. SOAP CRUD HypermediaREpresentational State TransferREST166API Survey - Style (Now)24 %39 %38 % 233. 28 %21 %14 %7 %REpresentational State TransferREST167API Survey - Plans to add0 %Hypermedia SOAP CRUD 234. References - REST Core http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm https://groups.yahoo.com/neo/groups/rest-discuss/conversations/topics/6735 http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven http://www.infoq.com/presentations/The-Counterintuitive-Web http://ruben.verborgh.org/blog/2012/08/24/rest-wheres-my-state/ https://code.google.com/p/implementing-rest/wiki/Video http://channel9.msdn.com/posts/TechTalk-RESTful-Application-Protocols-From-Design-to-REpresentational State TransferREST168Implementation http://www.infoq.com/presentations/qcon-tilkov-rest-intro http://www.infoq.com/presentations/vinoski-rest-serendipity http://bitworking.org/news/373/An-Introduction-to-REST http://vimeo.com/15697358 http://www.infoq.com/presentations/vinoski-rpc-convenient-but-flawed https://www.youtube.com/watch?v=llpr5924N7E https://www.youtube.com/watch?v=8Q7GyIBrysA http://steve.vinoski.net/pdf/IEEE-Demystifying_RESTful_Data_Coupling.pdf http://www.crummy.com/writing/speaking/2008-QCon/act3.html http://martinfowler.com/articles/richardsonMaturityModel.html 235. References - HTTP & URI https://www.mnot.net/blog/2014/06/07/rfc2616_is_dead http://www.ietf.org/rfc/rfc2616.txt http://tools.ietf.org/html/rfc7230 http://tools.ietf.org/html/rfc7231 http://tools.ietf.org/html/rfc7234 https://www.mnot.net/blog/2013/05/15/http_problem https://www.mnot.net/blog/2012/12/04/api-evolution https://www.mnot.net/blog/2012/09/05/patch https://www.mnot.net/blog/2012/06/25/http_api_complexity_model https://www.mnot.net/blog/2012/04/17/profiles https://www.mnot.net/blog/2009/02/18/x- http://tools.ietf.org/html/rfc3986 https://tools.ietf.org/html/rfc5789 http://tools.ietf.org/html/rfc6570REpresentational State TransferREST169 236. References -Hypermedia & APIs https://www.mnot.net/blog/2013/06/23/linking_apis http://oredev.org/2010/sessions/hypermedia-apis http://vimeo.com/75106815 https://www.innoq.com/blog/st/2012/06/hypermedia-benefits-for-m2m-communication/ http://ws-rest.org/2014/sites/default/files/wsrest2014_submission_12.pdf http://www.infoq.com/news/2014/03/ca-api-survey https://twitter.com/hypermediaapis https://www.youtube.com/watch?v=hdSrT4yjS1g https://www.youtube.com/watch?v=mZ8_QgJ5mbs http://nordsc.com/ext/classification_of_http_based_apis.html http://soabits.blogspot.no/2013/12/selling-benefits-of-hypermedia.html https://github.com/mamund/Building-Hypermedia-APIs http://amundsen.com/hypermedia/hfactor/ http://tech.blog.box.com/2013/04/get-developer-hugs-with-rich-error-handling-in-your-api/REpresentational State TransferREST170 237. References - Media Types http://stateless.co/hal_specification.html https://github.com/kevinswiber/siren https://github.com/JornWildt/Mason http://json-ld.org/ http://amundsen.com/media-types/collection/ http://soabits.blogspot.com/2013/12/media-types-for-apis.html http://soabits.blogspot.no/2013/05/the-role-of-media-types-in-restful-web.REpresentational State TransferREST171html http://soabits.blogspot.com/2014/03/modelling-shipment-example-as.html http://soabits.blogspot.com/2014/02/representing-issue-tracker-with-mason.html https://github.com/mamund/media-types/blob/master/uber-hypermedia.asciidoc 238. References - Versioning https://www.mnot.net/blog/2012/12/04/api-evolution https://www.mnot.net/blog/2012/07/11/header_versioning https://www.mnot.net/blog/2011/10/25/web_api_versioning_smackdown http://www.infoq.com/news/2013/12/api-versioning http://www.infoq.com/articles/Web-Service-Contracts http://www.infoq.com/news/2013/09/versioning-restful-services http://www.infoq.com/news/2010/06/rest-versioning http://www.ebpml.org/blog2/index.php/2013/11/25/understanding-the-costs-REpresentational State TransferREST172of-versioning http://www.troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html http://www.infoq.com/articles/contract-versioning-comp2 239. REpresentational State TransferREST173References - Caching https://www.mnot.net/cache_docs/ https://www.mnot.net/blog/2007/05/15/expires_max-age http://tools.ietf.org/html/rfc7232 https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers https://devcenter.heroku.com/articles/jax-rs-http-caching 240. REpresentational State TransferREST174References -Tutorials & Tools https://jax-rs-spec.java.net/ http://www.oracle.com/technetwork/articles/java/jaxrs20-1929352.html http://resteasy.jboss.org/ https://code.google.com/p/siren4j/ http://gotohal.net/ https://www.youtube.com/watch?v=1wEp9yHHtwg https://www.youtube.com/watch?v=sVvL12BnIyQ https://www.youtube.com/watch?v=pCnXy2Hs2Ag https://www.youtube.com/watch?v=_0kmqtWYvaY http://kingsfleet.blogspot.com/2014/02/transparent-patch-support-in-jax-rs-20.html http://spring.io/guides/tutorials/rest/ https://jaxb.java.net/ https://github.com/FasterXML/jackson 241. REST175Thanks!Questions?