" What needs to be done to make the REST architectural style - - PowerPoint PPT Presentation

what needs to be done to make the rest architectural
SMART_READER_LITE
LIVE PREVIEW

" What needs to be done to make the REST architectural style - - PowerPoint PPT Presentation

" What needs to be done to make the REST architectural style clear on the notion that hypertext is a constraint? In other words, if the engine of application state (and hence the API) is not being driven by hypertext, then it cannot


slide-1
SLIDE 1

"

What needs to be done to make the REST architectural style clear on the notion that hypertext is a constraint? In

  • ther words, if the engine of application state (and hence

the API) is not being driven by hypertext, then it cannot be RESTful and cannot be a REST API. Period. Is there some broken manual somewhere that needs to be fixed?
 


– Roy T. Fielding

slide-2
SLIDE 2

REST Assured

Hypermedia APIs with Spring

  • Oliver Gierke
slide-3
SLIDE 3

Oliver Gierke

Engineer @ Pivotal Spring Data project lead

  • gierke@gopivotal.com

www.olivergierke.de

  • livergierke
slide-4
SLIDE 4

Background

slide-5
SLIDE 5

REST

slide-6
SLIDE 6

REST

Resources URIs Uniform Interface Representations

slide-7
SLIDE 7

Hypermedia

slide-8
SLIDE 8

"

HATEOAS - the word, there's no pronunciation for.

  • – Ben Hale (SpringOne2GX 2012)
slide-9
SLIDE 9

Hypermedia

Links in representations State navigations discoverable

slide-10
SLIDE 10

{ _links : { self : { href : … }, cancel : { href : … }, update : { href : … }, payment : { href : "…/orders/4711/payment" } },

  • items : [ {

… } ],

  • location : "take-away",

price : 4.2, status : "payment expected" }

slide-11
SLIDE 11

MediaType

text/html application/hal+(json|xml) application/collection+json

slide-12
SLIDE 12

Implementation aspects

slide-13
SLIDE 13

Hypermedia VS. Java Frameworks

slide-14
SLIDE 14

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

✓ ✓ ✓ ✓ ✓ ✓

? ?

slide-15
SLIDE 15

Spring HATEOAS

slide-16
SLIDE 16

Spring HATEOAS

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

slide-17
SLIDE 17

DEMO

https:/ /github.com/olivergierke/spring- hateoas-sample

slide-18
SLIDE 18

Spring Data REST

slide-19
SLIDE 19

Spring Data REST

Export Spring Data repositories in a hypermedia-driven way Do „the right thing™“ by default

slide-20
SLIDE 20

DEMO

https:/ /github.com/olivergierke/rest- microservices

slide-21
SLIDE 21

REST in practice

slide-22
SLIDE 22
slide-23
SLIDE 23

RESTBucks

slide-24
SLIDE 24

RESTBucks

Starbucks (like) coffee ordering Order / Payment

slide-25
SLIDE 25

payment expected preparing cancelled ready completed

1 2 3 4 5 6

slide-26
SLIDE 26

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

(only if "payment expected")

4 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-27
SLIDE 27

Challenges

slide-28
SLIDE 28

Challenges

How to avoid hard coding URIs?

slide-29
SLIDE 29

Use link relations

slide-30
SLIDE 30
  • rders

Returns all orders available in the system

  • rder

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-31
SLIDE 31
  • rders

Returns all orders available in the system

  • rder

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-32
SLIDE 32

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

(only if "payment expected")

4 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-33
SLIDE 33

Method Relation type Action Step POST

  • rders

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

(only if "payment expected")

4 Barista preparing the order GET

  • rder

Poll order state 5 GET receipt Access receipt DELETE receipt Conclude the order process 6

slide-34
SLIDE 34

Challenges

How to implement:
 "only if payment expected“?

  • Clients react on the presence of links
slide-35
SLIDE 35

Spring RESTBucks

slide-36
SLIDE 36

Spring RESTBucks

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

slide-37
SLIDE 37

Web Service Repository

  • Orders

Spring Data Spring Data
 REST Payment Spring Data Manual
 implementation Manual
 implementation

slide-38
SLIDE 38

DEMO

https:/ /github.com/olivergierke/spring- restbucks

slide-39
SLIDE 39

API docs?

slide-40
SLIDE 40
slide-41
SLIDE 41

"

How does the client make sense of all this?

slide-42
SLIDE 42

Profiles

slide-43
SLIDE 43

Profiles

RFC 6906 Points to resources describing additional semantics within a media type

slide-44
SLIDE 44

Curies

slide-45
SLIDE 45

(HAL) Curies

slide-46
SLIDE 46

{ "_links" : { "self" : { "href" : … }, "restbucks:cancel" : { "href" : … }, "restbucks:update" : { "href" : … },

  • "curies" : {

"name" : "restbucks", "href" : "…/rels/{rel}" "templated" : true } }, … }

Docs for update are at: …/rels/update

slide-47
SLIDE 47

alps.io

slide-48
SLIDE 48

alps.io

Application Level Profile Semantics Describe state transitions and payloads Media type agnostic Repository with pre-defined docs application/alps+(json|xml)

slide-49
SLIDE 49

DEMO

slide-50
SLIDE 50

Miscellaneous

Spring MVC integration testing REST Shell

slide-51
SLIDE 51

Thank you!

slide-52
SLIDE 52

Resources

slide-53
SLIDE 53

Code

Spring HATEOAS Sample REST micro-services Spring RESTBucks

slide-54
SLIDE 54

Books

RESTful Web APIs REST in Practice REST und HTTP

slide-55
SLIDE 55

Videos

Hypermedia APIs - Jon Moore Hypermedia APIs with Spring