5. What is a Web Service?
Application integration based on open
standards (HTTP, XML)
Published Interface
Application functionality packaged as a
single unit and exposed to the network
19. XML Example
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
35. Principles
• Give every “thing” an ID
• Link things together
• Use standard methods
• Communicate stateless
• Resources with multiple representations
37. Link Things Together
<order self='http://example.com/customers/1234' >
<amount>23</amount>
<product ref='http://example.com/products/4554' />
<customer ref='http://example.com/customers/1234' />
</order>
38. Standard Methods
GET PUT POST DELETE
Collection URI List the URIs Replace the Create a new Delete the
http://ex.com/ and perhaps entire collection entry entire collection
customer other details
Element URI Retrieve a Update or if it Element as Delete the
http://ex.com/ representation doesn't exist, collection, entire element
customer/123 create it. create a new
entry in it, or
partial update
GET = Safe, only retrieval, caching
PUT, DELETE = Idempotent
40. Resources
• Can be anything - things, not actions
• Resources live on server
• Representations are transfered to clients
• Multiple representations of resources for
different needs
41. Content Type
• Multiple representations of resources for
different needs
• Mime-types (Accept header)
– text/xml
– application/json
– application/vnd.mycompany.customer+xml
– text/x-vcard
55. Restbucks
GET a Cup Of Coffee
Make selection
Pay
Wait for a while
Collect drink
56. Restbucks - Normal Flow
Customer POST http://restbucks.com/order Restbucks
201 + order
57. Restbucks - Normal Flow
Customer POST http://restbucks.com/order Restbucks
201 + order
PUT http://restbucks.com/payment/1234
201 + receipt
58. Restbucks - Normal Flow
Customer POST http://restbucks.com/order Restbucks
201 + order
PUT http://restbucks.com/payment/1234
201 + receipt
GET http://restbucks.com/order/1234
200 + order
59. Restbucks - Normal Flow
Customer POST http://restbucks.com/order Restbucks
201 + order
PUT http://restbucks.com/payment/1234
201 + receipt
GET http://restbucks.com/order/1234
200 + order
DELETE http://restbucks.com/receipt/1234
200 + completed order
60. Restbucks - Cancel
Customer POST http://restbucks.com/order Restbucks
201 + order
DELETE http://restbucks.com/order/1234
200 + order
61. Restbucks - Update
Customer POST http://restbucks.com/order Restbucks
201 + order
POST http://restbucks.com/order/1234
200 + order
69. Restbucks - Conflict
Customer POST http://restbucks.com/order Restbucks
201 + order
PUT http://restbucks.com/payment/1234
201 + receipt
POST http://restbucks.com/order/1234
409 Conflict
71. rel attribute
• Sematics of the referred resource
• Client must know meaning of rel, not uri
• Part of media type specification
<link uri=″http://restbucks.com/payment/1234″ rel=″payment″/>
72. rel attribute
• Sematics of the referred resource
• Client must know meaning of rel, not uri
• Part of media type specification
payment:
The linked resource allows the consumer to begin paying
for the order. Initiating payment involves PUTting an
appropriate resource representation to the specified
URI, as defined in the Restbucks media type.
<link uri=″http://restbucks.com/payment/1234″ rel=″payment″/>
79. Evolving Interfaces
Why?
• Impossible to predict the future
• All cannot jump simultaneously
Advice:
• You need a versioning strategy
• Know your consumers
• Be pragmatic
80. Types of Changes
• 2.1.3
• major.minor.point
• major = not compatible (new xml ns)
• minor = compatible
• point = no change to contract
81. Compatibility scenarios
• No compatibility
• Backwards Compatible
– old v1.0 consumer can use new v1.1 provider
• Forwards Compatible
– new v1.1 consumer can use old v1.0 provider
82. Not Backwards Compatible
Removing an operation
Renaming an operation
Changing the parameters of an operation
Changing the structure of a data type
95. Versioning Example
V 1.1
<account>
<name>Inigo Montoya</name>
<email-address>mailto:prepare-to-die@youkilledmyfather.
</account>
V 1.0 consumers can ignore email-address
96. Versioning Example
V 2.0
<account>
<name>Inigo Montoya</name>
<email-addresses>
<email-address priority='1'>mailto:prepare-to-die@youkil
<email-address priority='2'>mailto:vengeance@youkilledm
<email-address>
</account>
Will break v1.1 consumers
97. Use Different URLs
V 1.1 client use
http://foo.example/api/v1/accounts/3
V 2.0 client use
http://foo.example/api/v2/accounts/3
http://foo.example/api/accounts/3?v=2
98. Use Different URLs
. ..
V 1.1 client use ut
, b
http://foo.example/api/v1/accounts/3
le
V 2.0 client use ss
ib
P o
http://foo.example/api/v2/accounts/3
http://foo.example/api/accounts/3?v=2
Clients must support both versions if they store URLs
99. Vendor MIME type
• application/vnd.mycompany.myapp+xml
• Accept header of request
• Content-Type header of response
100. Content type negotiation
V 1.1 client ask for
Accept: application/vnd.mycompany.myapp+xml
V 2.0 client ask for
Accept: application/vnd.mycompany.myapp-v2+xml
101. Several Providers
Provider v1
Consumer
Provider v2
Accept: application/vnd.myapp-v2+xml, application/
vnd.myapp-v1+xml;q=0.8
Provider v1 answers with
Content-Type: application/vnd.myapp-v1+xml
106. Alice Gateway Cache Backend
GET /foo
Host: foo.com
107. Alice Gateway Cache Backend
GET /foo GET /foo
Host: foo.com Host: foo.com
108. Alice Gateway Cache Backend
GET /foo GET /foo
Host: foo.com Host: foo.com
200 OK
Cache‐Control: public, max‐age=60
ETag: abcdef012345
Hello World
109. Alice Gateway Cache Backend
GET /foo GET /foo
Host: foo.com Host: foo.com
200 OK 200 OK
Cache‐Control: public, max‐age=60 Cache‐Control: public, max‐age=60
ETag: abcdef012345 ETag: abcdef012345
Hello World Hello World
120. Contract first vs last
More up-front work
Need to learn all XML stuff
Decoupling
Better control
121. Validation and Errors
• Schema validation
• SOAP Fault
• Validation errors part of response
structure
122. HTTP Status Codes
200 - OK
201 - Created
301 - Moved
304 - Not modified
400 - Bad request
401 - Unauthorized
404 - Not found
405 - Method not allowed
409 - Conflict
500 - Internal Error
503 - Service Unavailable