Slide 1

Slide 1 text

© 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission. A hypermedia-driven REST web service Oliver Gierke Spring RESTBucks

Slide 2

Slide 2 text

Oliver Gierke Spring Data engineer Core / JPA / Mongo JPA Expert Group [email protected] www.olivergierke.de olivergierke

Slide 3

Slide 3 text

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!

Slide 4

Slide 4 text

Agenda I RESTBucks Spring Data Introduction Advanced Spring Data

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

RESTBucks An introduction

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

RESTBucks

Slide 9

Slide 9 text

RESTBucks Starbucks (like) coffee ordering Order / Payment

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

payment expected 1

Slide 12

Slide 12 text

payment expected 1 2

Slide 13

Slide 13 text

payment expected cancelled 1 2 3

Slide 14

Slide 14 text

payment expected preparing cancelled 1 2 3 4

Slide 15

Slide 15 text

payment expected preparing cancelled ready 1 2 3 4 5

Slide 16

Slide 16 text

payment expected preparing cancelled ready completed 1 2 3 4 5 6

Slide 17

Slide 17 text

Spring RESTBucks

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Technologies Spring Framework (4.0 M3) Spring Data JPA (1.4.1 / Babbage) Spring Data REST (2.0 snapshots) Spring HATEOAS (0.8)

Slide 20

Slide 20 text

Web Service Repository - Orders Spring Data Spring Data REST Payment Spring Data Manual implementation Manual implementation

Slide 21

Slide 21 text

Demo

Slide 22

Slide 22 text

Web Service Repository - Orders Spring Data Spring Data REST Payment Spring Data Manual implementation Manual implementation

Slide 23

Slide 23 text

Spring Data Convenient data access layers

Slide 24

Slide 24 text

Spring Data General introduction Repositories abstraction

Slide 25

Slide 25 text

Lab

Slide 26

Slide 26 text

Lab Setup Spring through JavaConfig Setup JPA in Spring context Enable Spring Data JPA Write integration tests for data access

Slide 27

Slide 27 text

Spring Data REST Easily export repositories to the web

Slide 28

Slide 28 text

REST Basics

Slide 29

Slide 29 text

REST in a nutshell Resources Identifier Uniform interface Representations Hypermedia

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Hypermedia Clients driven by links and their relation types

Slide 32

Slide 32 text

Implementation Patterns Resource per aggregate root GET, PUT, POST, DELETE Relations as links Media type design

Slide 33

Slide 33 text

Spring Data REST

Slide 34

Slide 34 text

Spring Data REST Export Spring Data repositories to the web Allow configuration and extension

Slide 35

Slide 35 text

Lab

Slide 36

Slide 36 text

Lab Setup Servlet 3.0 web app with Spring Activate Spring Data REST Spring MVC integration test setup Write web integration tests

Slide 37

Slide 37 text

Hypermedia Diving into hypermedia details

Slide 38

Slide 38 text

payment expected preparing cancelled ready completed 1 2 3 4 5 6

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Challenges

Slide 41

Slide 41 text

Challenges How to avoid hard coding URIs?

Slide 42

Slide 42 text

Use link relations

Slide 43

Slide 43 text

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).

Slide 44

Slide 44 text

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).

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Challenges How to implement: "only if payment expected"?

Slide 48

Slide 48 text

Process modeling

Slide 49

Slide 49 text

Root resource The only URI known GET /

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

{ 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" } }

Slide 54

Slide 54 text

Trigger payment Follow payment link $.links[?(@.rel="payment")].href PUT /orders/4711/payment

Slide 55

Slide 55 text

{ 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" } }

Slide 56

Slide 56 text

{ 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 } } }

Slide 57

Slide 57 text

Poll order Follow order link $.links[?(@.rel="order")].href GET /orders/4711 ETag / If-None-Match

Slide 58

Slide 58 text

{ 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 } } }

Slide 59

Slide 59 text

{ 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" } }

Slide 60

Slide 60 text

Poll order Use caching ETag / If-None-Match until…

Slide 61

Slide 61 text

{ 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" } }

Slide 62

Slide 62 text

Access receipt Follow receipt link $.links[?(@.rel="receipt")].href GET /orders/4711/receipt

Slide 63

Slide 63 text

{ 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" } }

Slide 64

Slide 64 text

Conclude order Follow receipt link $.links[?(@.rel="receipt")].href DELETE /orders/4711/receipt

Slide 65

Slide 65 text

Background

Slide 66

Slide 66 text

Hypermedia VS. Java Frameworks

Slide 67

Slide 67 text

HTTP Methods URI Mapping Content negotiation Hypermedia Spring MVC JAX-RS ✓ ✓ ✓ ✓ ✓ ✓ ? ?

Slide 68

Slide 68 text

Spring HATEOAS

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

DEMO

Slide 71

Slide 71 text

DEMO

Slide 72

Slide 72 text

Miscellaneous Spring MVC integration testing REST Shell

Slide 73

Slide 73 text

Spring Data REST

Slide 74

Slide 74 text

Spring Data REST Exports JPA repositories as resources Hypermedia driven representations Extension points http://bit.ly/sd-rest

Slide 75

Slide 75 text

REST Shell

Slide 76

Slide 76 text

Thank you!

Slide 77

Slide 77 text

Resources

Slide 78

Slide 78 text

Further talks Spring Data Repositories - Best practices - Wed 10:30am Multi Client Development with Spring - Wed 4:30pm

Slide 79

Slide 79 text

Code Spring HATEOAS Sample Spring RESTBucks Spring Data REST Spring HATEOAS REST Shell

Slide 80

Slide 80 text

Books REST in Practice REST und HTTP RESTful WebServices Cookbook Spring Data

Slide 81

Slide 81 text

Videos Hypermedia APIs - Jon Moore Hypermedia APIs with Spring