Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring RESTBucks - A Hypermedia REST Web Service

Spring RESTBucks - A Hypermedia REST Web Service

977c74bb044a9d4fa90b305824eda390?s=128

Oliver Drotbohm

September 10, 2013
Tweet

Transcript

  1. © 2013 SpringOne 2GX. All rights reserved. Do not distribute

    without permission. A hypermedia-driven REST web service Oliver Gierke Spring RESTBucks
  2. Oliver Gierke Spring Data engineer Core / JPA / Mongo

    JPA Expert Group ogierke@vmware.com www.olivergierke.de olivergierke
  3. Spring Data Modern Data Access For Enterprise Java NoSQL JPA

    JDBC Redis Big Data Hadoop HBase MongoDB Neo4j REST exporter Roo Hive Pig Querydsl Repositories Gemfire Splunk Free e-book for every attendee!
  4. Agenda I RESTBucks Spring Data Introduction Advanced Spring Data

  5. Agenda II Spring Data REST Spring MVC integration testing Advanced

    Spring Data REST
  6. RESTBucks An introduction

  7. None
  8. RESTBucks

  9. RESTBucks Starbucks (like) coffee ordering Order / Payment

  10. None
  11. payment expected 1

  12. payment expected 1 2

  13. payment expected cancelled 1 2 3

  14. payment expected preparing cancelled 1 2 3 4

  15. payment expected preparing cancelled ready 1 2 3 4 5

  16. payment expected preparing cancelled ready completed 1 2 3 4

    5 6
  17. Spring RESTBucks

  18. Spring RESTBucks Sample implementation Using Spring technologies http://bit.ly/spring-restbucks

  19. Technologies Spring Framework (4.0 M3) Spring Data JPA (1.4.1 /

    Babbage) Spring Data REST (2.0 snapshots) Spring HATEOAS (0.8)
  20. Web Service Repository - Orders Spring Data Spring Data REST

    Payment Spring Data Manual implementation Manual implementation
  21. Demo

  22. Web Service Repository - Orders Spring Data Spring Data REST

    Payment Spring Data Manual implementation Manual implementation
  23. Spring Data Convenient data access layers

  24. Spring Data General introduction Repositories abstraction

  25. Lab

  26. Lab Setup Spring through JavaConfig Setup JPA in Spring context

    Enable Spring Data JPA Write integration tests for data access
  27. Spring Data REST Easily export repositories to the web

  28. REST Basics

  29. REST in a nutshell Resources Identifier Uniform interface Representations Hypermedia

  30. "HATEOAS - the word, there's no pronounciation for. (Ben Hale,

    SpringOne2GX 2012)
  31. Hypermedia Clients driven by links and their relation types

  32. Implementation Patterns Resource per aggregate root GET, PUT, POST, DELETE

    Relations as links Media type design
  33. Spring Data REST

  34. Spring Data REST Export Spring Data repositories to the web

    Allow configuration and extension
  35. Lab

  36. Lab Setup Servlet 3.0 web app with Spring Activate Spring

    Data REST Spring MVC integration test setup Write web integration tests
  37. Hypermedia Diving into hypermedia details

  38. payment expected preparing cancelled ready completed 1 2 3 4

    5 6
  39. Method URI Action Step POST /orders Create new order 1

    POST/PATCH /orders/4711 Update the order (only if "payment expected") 2 DELETE /orders/4711 Cancel order (only if "payment expected") 3 PUT /orders/4711/payment Pay order 4 Barista preparing the order Barista preparing the order Barista preparing the order Barista preparing the order GET /orders/4711 Poll order state 5 GET /orders/4711/receipt Access receipt DELETE /orders/4711/receipt Conclude the order process 6
  40. Challenges

  41. Challenges How to avoid hard coding URIs?

  42. Use link relations

  43. orders Returns all orders available in the system order Returns

    a single order self The uri value can be used to GET the latest resource representation of the order. cancel This is the URI to be used to DELETE the order resource should the consumer wish to cancel the order. update Consumers can change the order using a POST to transfer a representation to the linked resource. payment The linked resource allows the consumer to begin paying for an order. Initiating payment involves PUTting an appropriate resource representation to the specified URI. receipt The URI to access the receipt using GET and conclude the order by taking the receipt (use DELETE).
  44. orders Returns all orders available in the system order Returns

    a single order self The uri value can be used to GET the latest resource representation of the order. cancel This is the URI to be used to DELETE the order resource should the consumer wish to cancel the order. update Consumers can change the order using a POST to transfer a representation to the linked resource. payment The linked resource allows the consumer to begin paying for an order. Initiating payment involves PUTting an appropriate resource representation to the specified URI. receipt The URI to access the receipt using GET and conclude the order by taking the receipt (use DELETE).
  45. Method URI Action Step POST /orders Create new order 1

    POST/PATCH /orders/4711 Update the order (only if "payment expected") 2 DELETE /orders/4711 Cancel order (only if "payment expected") 3 PUT /orders/4711/payment Pay order 4 Barista preparing the order Barista preparing the order Barista preparing the order Barista preparing the order GET /orders/4711 Poll order state 5 GET /orders/4711/receipt Access receipt DELETE /orders/4711/receipt Conclude the order process 6
  46. Method Relation type Action Step POST orders Create new order

    1 POST/PATCH update Update the order (only if "payment expected") 2 DELETE cancel Cancel order (only if "payment expected") 3 PUT payment Pay order 4 Barista preparing the order Barista preparing the order Barista preparing the order Barista preparing the order GET order Poll order state 5 GET receipt Access receipt DELETE receipt Conclude the order process 6
  47. Challenges How to implement: "only if payment expected"?

  48. Process modeling

  49. Root resource The only URI known GET /

  50. { links : [ { rel : "orders", href :

    "…/orders" } ] }
  51. Place order Access root resource Follow orders link $.links[?(@.rel="orders")].href POST

    /orders
  52. { links : [ { rel : "orders", href :

    "…/orders" } ] }
  53. { links : [ { rel : "self", href :

    … }, { rel : "cancel", href : … }, { rel : "update", href : … }, { rel : "payment", href : "…/orders/4711/payment" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "payment expected" } }
  54. Trigger payment Follow payment link $.links[?(@.rel="payment")].href PUT /orders/4711/payment

  55. { links : [ { rel : "self", href :

    "…/orders/4711" }, … { rel : "payment", href : "…/orders/4711/payment" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "payment expected" } }
  56. { links : [ { rel : "self", href :

    "…/orders/4711/payment" }, { rel : "order", href : "…/orders/4711" } ], content : { creditCard : [ { number : "1234123412341234", cardHolder : "Oliver Gierke", expiryDate : "2013-11-01" } ], amount : { currency : "EUR", value : 4.2 } } }
  57. Poll order Follow order link $.links[?(@.rel="order")].href GET /orders/4711 ETag /

    If-None-Match
  58. { links : [ { rel : "self", href :

    "…/orders/4711/payment" }, { rel : "order", href : "…/orders/4711" } ], content : { creditCard : [ { number : "1234123412341234", cardHolder : "Oliver Gierke", expiryDate : "2013-11-01" } ], amount : { currency : "EUR", value : 4.2 } } }
  59. { links : [ { rel : "self", href :

    "…/orders/4711" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "preparing" } }
  60. Poll order Use caching ETag / If-None-Match until…

  61. { links : [ { rel : "self", href :

    "…/orders/4711" }, { rel : "receipt", href : "…/orders/4711/receipt" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "ready" } }
  62. Access receipt Follow receipt link $.links[?(@.rel="receipt")].href GET /orders/4711/receipt

  63. { links : [ { rel : "self", href :

    "…/orders/4711" }, { rel : "receipt", href : "…/orders/4711/receipt" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "ready" } }
  64. Conclude order Follow receipt link $.links[?(@.rel="receipt")].href DELETE /orders/4711/receipt

  65. Background

  66. Hypermedia VS. Java Frameworks

  67. HTTP Methods URI Mapping Content negotiation Hypermedia Spring MVC JAX-RS

    ✓ ✓ ✓ ✓ ✓ ✓ ? ?
  68. Spring HATEOAS

  69. Spring HATEOAS Representation models LinkBuilder API Representation enrichment http://bit.ly/spring-hateoas

  70. DEMO

  71. DEMO

  72. Miscellaneous Spring MVC integration testing REST Shell

  73. Spring Data REST

  74. Spring Data REST Exports JPA repositories as resources Hypermedia driven

    representations Extension points http://bit.ly/sd-rest
  75. REST Shell

  76. Thank you!

  77. Resources

  78. Further talks Spring Data Repositories - Best practices - Wed

    10:30am Multi Client Development with Spring - Wed 4:30pm
  79. Code Spring HATEOAS Sample Spring RESTBucks Spring Data REST Spring

    HATEOAS REST Shell
  80. Books REST in Practice REST und HTTP RESTful WebServices Cookbook

    Spring Data
  81. Videos Hypermedia APIs - Jon Moore Hypermedia APIs with Spring