Spring RESTBucks - A hypermedia-driven REST webservice
Upcoming SlideShare
Loading in...5
×
 

Spring RESTBucks - A hypermedia-driven REST webservice

on

  • 1,878 views

Speakers: Oliver Gierke ...

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.

Statistics

Views

Total Views
1,878
Views on SlideShare
1,878
Embed Views
0

Actions

Likes
1
Downloads
26
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Spring RESTBucks - A hypermedia-driven REST webservice Spring RESTBucks - A hypermedia-driven REST webservice Presentation Transcript

  • Spring RESTBucks A hypermedia-driven REST web service Oliver Gierke © 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.
  • Oliver Gierke Spring Data engineer Core / JPA / Mongo JPA Expert Group ogierke@vmware.com www.olivergierke.de olivergierke
  • 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!
  • Agenda I RESTBucks Spring Data Introduction Advanced Spring Data
  • Agenda II Spring Data REST Spring MVC integration testing Advanced Spring Data REST
  • RESTBucks An introduction
  • RESTBucks
  • RESTBucks Starbucks (like) coffee ordering Order / Payment
  • 1 payment expected
  • 2 1 payment expected
  • 2 1 payment expected 3 cancelled
  • 2 preparing 4 1 payment expected 3 cancelled
  • 5 2 preparing 4 1 payment expected 3 cancelled ready
  • 5 2 preparing 4 1 payment expected 3 cancelled 6 ready completed
  • Spring RESTBucks
  • Spring RESTBucks Sample implementation Using Spring technologies http://bit.ly/spring-restbucks
  • Technologies Spring Framework (4.0 M3) Spring Data JPA (1.4.1 / Babbage) Spring Data REST (2.0 snapshots) Spring HATEOAS (0.8)
  • Orders Payment Web Spring Data REST Manual implementation Service - Manual implementation Repository Spring Data Spring Data
  • Demo
  • Orders Payment Web Spring Data REST Manual implementation Service - Manual implementation Repository Spring Data Spring Data
  • Spring Data Convenient data access layers
  • Spring Data General introduction Repositories abstraction
  • Lab
  • Lab Setup Spring through JavaConfig Setup JPA in Spring context Enable Spring Data JPA Write integration tests for data access
  • Spring Data REST Easily export repositories to the web
  • REST Basics
  • REST in a nutshell Resources Identifier Uniform interface Representations Hypermedia
  • " HATEOAS - the word, there's no pronounciation for. (Ben Hale, SpringOne2GX 2012)
  • Hypermedia Clients driven by links and their relation types
  • Implementation Patterns Resource per aggregate root GET, PUT, POST, DELETE Relations as links Media type design
  • Spring Data REST
  • Spring Data REST Export Spring Data repositories to the web Allow configuration and extension
  • Lab
  • Lab Setup Servlet 3.0 web app with Spring Activate Spring Data REST Spring MVC integration test setup Write web integration tests
  • Hypermedia Diving into hypermedia details
  • 5 2 preparing 4 1 payment expected 3 cancelled 6 ready completed
  • 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
  • Challenges
  • Challenges How to avoid hard coding URIs?
  • Use link relations
  • 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).
  • 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).
  • 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
  • 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
  • Challenges How to implement: "only if payment expected"?
  • Process modeling
  • Root resource The only URI known GET /
  • { links : [ { rel : "orders", href : "…/orders" } ] }
  • Place order Access root resource Follow orders link $.links[?(@.rel="orders")].href POST /orders
  • { links : [ { rel : "orders", href : "…/orders" } ] }
  • { 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" } }
  • Trigger payment Follow payment link $.links[?(@.rel="payment")].href PUT /orders/4711/payment
  • { 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" }
  • { 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 } }
  • Poll order Follow order link $.links[?(@.rel="order")].href GET /orders/4711 ETag / If-None-Match
  • { 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 } }
  • { 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"
  • Poll order Use caching ETag / If-None-Match until…
  • { 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" }
  • Access receipt Follow receipt link $.links[?(@.rel="receipt")].href GET /orders/4711/receipt
  • { 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" }
  • Conclude order Follow receipt link $.links[?(@.rel="receipt")].href DELETE /orders/4711/receipt
  • Background
  • Hypermedia VS. Java Frameworks
  • Spring MVC JAX-RS HTTP Methods ✓ ✓ URI Mapping ✓ ✓ Content negotiation ✓ ✓ Hypermedia ? ?
  • Spring HATEOAS
  • Spring HATEOAS Representation models LinkBuilder API Representation enrichment http://bit.ly/spring-hateoas
  • DEMO
  • DEMO
  • Miscellaneous Spring MVC integration testing REST Shell
  • Spring Data REST
  • Spring Data REST Exports JPA repositories as resources Hypermedia driven representations Extension points http://bit.ly/sd-rest
  • REST Shell
  • Thank you!
  • Resources
  • Further talks Spring Data Repositories - Best practices - Wed 10:30am Multi Client Development with Spring - Wed 4:30pm
  • Code Spring HATEOAS Sample Spring RESTBucks Spring Data REST Spring HATEOAS REST Shell
  • Books REST in Practice REST und HTTP RESTful WebServices Cookbook Spring Data
  • Videos Hypermedia APIs - Jon Moore Hypermedia APIs with Spring