Spring RESTBucks
A hypermedia-driven REST web service
Oliver Gierke

© 2013 SpringOne 2GX. All rights reserved. Do not dis...
Oliver Gierke
Spring Data engineer
Core / JPA / Mongo
JPA Expert Group

ogierke@vmware.com
www.olivergierke.de
olivergierk...
Spring Data
Modern Data Access For Enterprise Java

Hive
JPA

JDBC

HBase

Big Data

NoSQL

Redis
MongoDB
Querydsl

Pig

R...
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 (...
Orders

Payment

Web

Spring Data
REST

Manual
implementation

Service

-

Manual
implementation

Repository

Spring Data
...
Demo
Orders

Payment

Web

Spring Data
REST

Manual
implementation

Service

-

Manual
implementation

Repository

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 acc...
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 integratio...
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

/ord...
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 ...
orders
order
self

Returns all orders available in the system
Returns a single order
The uri value can be used to GET the ...
Method

URI

Action

Step

POST

/orders

Create new order

1

POST/PATCH

/orders/4711

Update the order

2

DELETE

/ord...
Method

Relation type

Action

Step

POST

orders

Create new order

1

POST/PATCH

update

Update the order

2

DELETE

c...
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",
hre...
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" } ],

conten...
{ links : [ { rel
href
{ rel
href

}

:
:
:
:

"self",
"…/orders/4711/payment" },
"order",
"…/orders/4711" } ],

content :...
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 :...
{ links : [ { rel : "self",
href : "…/orders/4711" } ],
content : {
items : [ {
drink : "Cappucino",
size : "large",
milk ...
Poll order
Use caching
ETag / If-None-Match
until…
{ links : [ { rel
href
{ rel
href

}

:
:
:
:

"self",
"…/orders/4711" },
"receipt",
"…/orders/4711/receipt" } ],

content...
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...
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 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
Spring RESTBucks - A hypermedia-driven REST webservice
Spring RESTBucks - A hypermedia-driven REST webservice
Upcoming SlideShare
Loading in …5
×

Spring RESTBucks - A hypermedia-driven REST webservice

2,820
-1

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
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,820
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
57
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×