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

Spring RESTBucks - A hypermedia-driven REST webservice

4,462 views

Published on

Speakers: Oliver Gierke
Spring MVC forms a solid foundation to implement REST based web-services in Java. However, in real-world projects developers still face challenges when it comes to advanced questions of REST. How to really leverage hypermedia? How to model more complex business functionality with REST. The talk introduces the Spring RESTBucks sample implementation of a hypermedia-driven REST web service and explains how it is using hypermedia elements to implement business processes and how Spring technologies (Spring Data REST, Spring Data JPA and Spring HATEOAS) help developers building the system.

Published in: Technology, Business
  • Be the first to comment

Spring RESTBucks - A hypermedia-driven REST webservice

  1. 1. Spring RESTBucks A hypermedia-driven REST web service Oliver Gierke © 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.
  2. 2. Oliver Gierke Spring Data engineer Core / JPA / Mongo JPA Expert Group ogierke@vmware.com www.olivergierke.de olivergierke
  3. 3. Spring Data Modern Data Access For Enterprise Java Hive JPA JDBC HBase Big Data NoSQL Redis MongoDB Querydsl Pig Roo Splunk Hadoop Gemfire REST exporter Neo4j Repositories Free e-book for every attendee!
  4. 4. Agenda I RESTBucks Spring Data Introduction Advanced Spring Data
  5. 5. Agenda II Spring Data REST Spring MVC integration testing Advanced Spring Data REST
  6. 6. RESTBucks An introduction
  7. 7. RESTBucks
  8. 8. RESTBucks Starbucks (like) coffee ordering Order / Payment
  9. 9. 1 payment expected
  10. 10. 2 1 payment expected
  11. 11. 2 1 payment expected 3 cancelled
  12. 12. 2 preparing 4 1 payment expected 3 cancelled
  13. 13. 5 2 preparing 4 1 payment expected 3 cancelled ready
  14. 14. 5 2 preparing 4 1 payment expected 3 cancelled 6 ready completed
  15. 15. Spring RESTBucks
  16. 16. Spring RESTBucks Sample implementation Using Spring technologies http://bit.ly/spring-restbucks
  17. 17. Technologies Spring Framework (4.0 M3) Spring Data JPA (1.4.1 / Babbage) Spring Data REST (2.0 snapshots) Spring HATEOAS (0.8)
  18. 18. Orders Payment Web Spring Data REST Manual implementation Service - Manual implementation Repository Spring Data Spring Data
  19. 19. Demo
  20. 20. Orders Payment Web Spring Data REST Manual implementation Service - Manual implementation Repository Spring Data Spring Data
  21. 21. Spring Data Convenient data access layers
  22. 22. Spring Data General introduction Repositories abstraction
  23. 23. Lab
  24. 24. Lab Setup Spring through JavaConfig Setup JPA in Spring context Enable Spring Data JPA Write integration tests for data access
  25. 25. Spring Data REST Easily export repositories to the web
  26. 26. REST Basics
  27. 27. REST in a nutshell Resources Identifier Uniform interface Representations Hypermedia
  28. 28. " HATEOAS - the word, there's no pronounciation for. (Ben Hale, SpringOne2GX 2012)
  29. 29. Hypermedia Clients driven by links and their relation types
  30. 30. Implementation Patterns Resource per aggregate root GET, PUT, POST, DELETE Relations as links Media type design
  31. 31. Spring Data REST
  32. 32. Spring Data REST Export Spring Data repositories to the web Allow configuration and extension
  33. 33. Lab
  34. 34. Lab Setup Servlet 3.0 web app with Spring Activate Spring Data REST Spring MVC integration test setup Write web integration tests
  35. 35. Hypermedia Diving into hypermedia details
  36. 36. 5 2 preparing 4 1 payment expected 3 cancelled 6 ready completed
  37. 37. Method URI Action Step POST /orders Create new order 1 POST/PATCH /orders/4711 Update the order 2 DELETE /orders/4711 PUT /orders/4711/payment (only if "payment expected") Cancel order (only if "payment expected") 3 Pay order 4 5 Barista preparing the order GET /orders/4711 Poll order state GET /orders/4711/receipt Access receipt DELETE /orders/4711/receipt Conclude the order process 6
  38. 38. Challenges
  39. 39. Challenges How to avoid hard coding URIs?
  40. 40. Use link relations
  41. 41. orders order self Returns all orders available in the system Returns a single order 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).
  42. 42. orders order self Returns all orders available in the system Returns a single order 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).
  43. 43. Method URI Action Step POST /orders Create new order 1 POST/PATCH /orders/4711 Update the order 2 DELETE /orders/4711 PUT /orders/4711/payment (only if "payment expected") Cancel order (only if "payment expected") 3 Pay order 4 5 Barista preparing the order GET /orders/4711 Poll order state GET /orders/4711/receipt Access receipt DELETE /orders/4711/receipt Conclude the order process 6
  44. 44. Method Relation type Action Step POST orders Create new order 1 POST/PATCH update Update the order 2 DELETE cancel PUT payment (only if "payment expected") Cancel order (only if "payment expected") 3 Pay order 4 5 Barista preparing the order GET order Poll order state GET receipt Access receipt DELETE receipt Conclude the order process 6
  45. 45. Challenges How to implement: "only if payment expected"?
  46. 46. Process modeling
  47. 47. Root resource The only URI known GET /
  48. 48. { links : [ { rel : "orders", href : "…/orders" } ] }
  49. 49. Place order Access root resource Follow orders link $.links[?(@.rel="orders")].href POST /orders
  50. 50. { links : [ { rel : "orders", href : "…/orders" } ] }
  51. 51. { 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" } }
  52. 52. Trigger payment Follow payment link $.links[?(@.rel="payment")].href PUT /orders/4711/payment
  53. 53. { links : [ { rel href … { rel href } : "self", : "…/orders/4711" }, : "payment", : "…/orders/4711/payment" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "payment expected" }
  54. 54. { links : [ { rel href { rel href } : : : : "self", "…/orders/4711/payment" }, "order", "…/orders/4711" } ], content : { creditCard : [ { number : "1234123412341234", cardHolder : "Oliver Gierke", expiryDate : "2013-11-01" } ], amount : { currency : "EUR", value : 4.2 } }
  55. 55. Poll order Follow order link $.links[?(@.rel="order")].href GET /orders/4711 ETag / If-None-Match
  56. 56. { links : [ { rel href { rel href } : : : : "self", "…/orders/4711/payment" }, "order", "…/orders/4711" } ], content : { creditCard : [ { number : "1234123412341234", cardHolder : "Oliver Gierke", expiryDate : "2013-11-01" } ], amount : { currency : "EUR", value : 4.2 } }
  57. 57. { 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"
  58. 58. Poll order Use caching ETag / If-None-Match until…
  59. 59. { links : [ { rel href { rel href } : : : : "self", "…/orders/4711" }, "receipt", "…/orders/4711/receipt" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "ready" }
  60. 60. Access receipt Follow receipt link $.links[?(@.rel="receipt")].href GET /orders/4711/receipt
  61. 61. { links : [ { rel href { rel href } : : : : "self", "…/orders/4711" }, "receipt", "…/orders/4711/receipt" } ], content : { items : [ { drink : "Cappucino", size : "large", milk : "semi" price : 4.2 } ], location : "take-away", price : 4.2 status : "ready" }
  62. 62. Conclude order Follow receipt link $.links[?(@.rel="receipt")].href DELETE /orders/4711/receipt
  63. 63. Background
  64. 64. Hypermedia VS. Java Frameworks
  65. 65. Spring MVC JAX-RS HTTP Methods ✓ ✓ URI Mapping ✓ ✓ Content negotiation ✓ ✓ Hypermedia ? ?
  66. 66. Spring HATEOAS
  67. 67. Spring HATEOAS Representation models LinkBuilder API Representation enrichment http://bit.ly/spring-hateoas
  68. 68. DEMO
  69. 69. DEMO
  70. 70. Miscellaneous Spring MVC integration testing REST Shell
  71. 71. Spring Data REST
  72. 72. Spring Data REST Exports JPA repositories as resources Hypermedia driven representations Extension points http://bit.ly/sd-rest
  73. 73. REST Shell
  74. 74. Thank you!
  75. 75. Resources
  76. 76. Further talks Spring Data Repositories - Best practices - Wed 10:30am Multi Client Development with Spring - Wed 4:30pm
  77. 77. Code Spring HATEOAS Sample Spring RESTBucks Spring Data REST Spring HATEOAS REST Shell
  78. 78. Books REST in Practice REST und HTTP RESTful WebServices Cookbook Spring Data
  79. 79. Videos Hypermedia APIs - Jon Moore Hypermedia APIs with Spring

×