SlideShare ist ein Scribd-Unternehmen logo
1 von 136
Downloaden Sie, um offline zu lesen
Web Services and REST




Patrik Nordwall
Roadmap

Introduction to Web Services

                Introduction to REST

Design Challenges
Introduction to Web Services




Patrik Nordwall
Outline


Concepts (SOAP, WSDL, ...)

      XML Schema

          Demo
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
Conceptual Model
SOAP

•   Simple Object Access Protocol
•   Service Invocation
•   Cross-platform remote calls
•   Usually XML over HTTP (POST)
SOAP Parts
SOAP Example - request
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://www.example.o
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>
SOAP Example - response
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelop
soap:encodingStyle="http://www.w3.org/2001/12/soap

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPriceResponse>
    <m:Price>34.5</m:Price>
  </m:GetStockPriceResponse>
</soap:Body>

</soap:Envelope>
WSDL
                                 ‘wiz-del’


• Web Service Description Language
• Machine-readable language
  For tools - not humans
WSDL Structure

•   portType - operations
•   messages
•   types
•   binding - communication protocols
WSDL Example
<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>
WSDL Binding

<binding type="glossaryTerms" name="b1">
   <soap:binding style="document"
   transport="http://schemas.xmlsoap.org/soap/http
   <operation>
     <soap:operation soapAction="http://example.co
     <input><soap:body use="literal"/></input>
     <output><soap:body use="literal"/></output>
  </operation>
</binding>
UDDI
• Universal Discovery and Directory
  Interface
• Kind of yellow pages
• You probably don’t need all UDDI
  features
XML Schema
 Data definition in XML format
Important for Service Contract
XML Example

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
XSD Example
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>
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>
XSD Types
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
XSD Types
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>


•   xs:string
•   xs:decimal
•   xs:integer
•   xs:boolean
•   xs:date
•   xs:time
XSD Restriction

<xs:element name="age" type="Age"/>

<xs:simpleType name="Age">
 <xs:restriction base="xs:integer">
   <xs:minInclusive value="0"/>
   <xs:maxInclusive value="120"/>
  </xs:restriction>
</xs:simpleType>
XSD Optional/Required


<xs:attribute name="lang" type="xs:string" use="required"/>

<xs:element name="body" type="xs:string" minOccurs="0"/>

<xs:element name="body" type="xs:string" nillable="true"/>
XSD Default Values

<xs:attribute name="lang" type="xs:string" default="EN"/>
XSD Any Order

<xs:element name="person">
 <xs:complexType>
  <xs:all>
   <xs:element name="firstname" type="xs:string"/>
   <xs:element name="lastname" type="xs:string"/>
  </xs:all>
 </xs:complexType>
</xs:element>
XSD Choice

<xs:element name="person">
 <xs:complexType>
  <xs:choice>
   <xs:element name="employee" type="employee"/>
   <xs:element name="member" type="member"/>
  </xs:choice>
 </xs:complexType>
</xs:element>
XSD Extension
<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">
 <xs:sequence>
  <xs:element name="firstname" type="xs:string"/>
  <xs:element name="lastname" type="xs:string"/>
 </xs:sequence>
</xs:complexType>

<xs:complexType name="fullpersoninfo">
 <xs:complexContent>
  <xs:extension base="personinfo">
   <xs:sequence>
     <xs:element name="address" type="xs:string"/>
     <xs:element name="city" type="xs:string"/>
     <xs:element name="country" type="xs:string"/>
   </xs:sequence>
  </xs:extension>
 </xs:complexContent>
</xs:complexType>
WS Tools

•   Apache CXF
•   Axis 2
•   Spring Web Services
•   Metro (GlassFish)
•   WebLogic (wsdlc)
•   JBossWS
•   ...
Demo
Spring Web
 Services
Introduction to REST




Patrik Nordwall
Outline

What is REST?

   JSON

   Demo
REST is NOT

  Architecture
   Protocol
  Technology
REST is an architectural style
REST

• Representational State Transfer
• 'RESTful' == Conforming to the REST
  constraints
Principles
•   Give every “thing” an ID
•   Link things together
•   Use standard methods
•   Communicate stateless
•   Resources with multiple representations
URIs

http://example.com/customers/1234
http://example.com/orders/2007/10/776654
http://example.com/products/4554
http://example.com/processes/salary-increase-234
http://example.com/products?color=green
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>
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
Stateless

Stateless communication

     State can live in resource state

                    State can live in client
Resources
•   Can be anything - things, not actions
•   Resources live on server
•   Representations are transfered to clients
•   Multiple representations of resources for
    different needs
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
JSON


Objects {}
    &
 Lists []
JSON

{
    "name" : "Patrik",
    "age" : 37,
    "parent" : true
}
JSON
[
    {
         "title": "Perpendicular",
         "artist": "Deep Purple"
    },
    {
         "title": "Dover Calais",
         "artist" : "Style"
    }
]
JSON
{
    "name" : "Patrik",
    "children" : [
      "Ebba",
      "Klara"
    ]
}
REST Tools

    Jersey
 Apache CXF
    Restlet
  RestEasy
  Spring 3.0
   Restfulie
Demo
Spring 3.0
  REST
Design Challenges
           of Web Services and
           RESTful Web Services




Patrik Nordwall
Outline

Hypermedia

    Evolving interfaces

Scaling out

     Other Challenges
REST
Hypermedia
Constraint
Hypermedia


         Link things together
           One entry point
 Links declare transitions to next step
Don’t need a static contract description
HATEOAS



    Hypermedia As The
Engine Of Application State
API Types

Infrastructure

Domain CRUD

 Application
URI Templates



http://example.com/products/{productNumber}
Restbucks

  GET a Cup Of Coffee
          Make selection
                     Pay
         Wait for a while
           Collect drink
Restbucks - Normal Flow

Customer   POST http://restbucks.com/order             Restbucks

                                         201 + order
Restbucks - Normal Flow

Customer   POST http://restbucks.com/order               Restbucks

                                         201 + order


           PUT http://restbucks.com/payment/1234
                                         201 + receipt
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
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
Restbucks - Cancel

Customer   POST http://restbucks.com/order             Restbucks

                                         201 + order


           DELETE http://restbucks.com/order/1234
                                         200 + order
Restbucks - Update

Customer   POST http://restbucks.com/order             Restbucks

                                         201 + order


           POST http://restbucks.com/order/1234
                                         200 + order
POST Order

<order xmlns=″http://schemas.restbucks.com″ >
 <item>
  <milk>semi</milk>
  <size>large</size>
  <drink>cappuccino</drink>
 </item>
 <location>takeAway</location>
</order>
Order Response
<order xmlns=″http://schemas.restbucks.com″ >
 <item>
  <milk>semi</milk>
  <size>large</size>
  <drink>cappuccino</drink>
 </item>
 <location>takeAway</location>
 <cost>2. 0</cost>
 <status>unpaid</status>
 <link uri=″http://restbucks.com/order/1234″ rel=″cancel″/>
 <link uri=″http://restbucks.com/payment/1234″ rel=″payment″/>
 <link uri=″http://restbucks.com/order/1234″ rel=″update″/>
 <link uri=″http://restbucks.com/order/1234″ rel=″self″/>
</order>
PUT Payment
<payment xmlns=″http://schemas.restbucks.com″>
 <amount>2. 0</amount>
 <cardholderName>Michael Faraday</cardholderName>
 <cardNumber>11223344</cardNumber>
 <expiryMonth>12</expiryMonth>
 <expiryYear>12</expiryYear>
</payment>
Payment Response
<payment xmlns=″http://schemas.restbucks.com″>
 <amount>2. 0</amount>
 <cardholderName>Michael Faraday</cardholderName>
 <cardNumber>11223344</cardNumber>
 <expiryMonth>12</expiryMonth>
 <expiryYear>12</expiryYear>
 <link uri=″http://restbucks.com/order/1234″ rel=″order″/>
 <link uri=″http: //restbucks.com/receipt/1234″ rel=″receipt″/>
</payment>
GET Order Response
<order xmlns=″http://schemas.restbucks.com″ >
 <item>
  <milk>semi</milk>
  <size>large</size>
  <drink>cappuccino</drink>
 </item>
 <location>takeAway</location>
 <cost>2. 0</cost>
 <status>preparing</status>
 <link uri=″http://restbucks.com/order/1234″ rel=″self″/>
</order>
...GET Order Response
<order xmlns=″http://schemas.restbucks.com″ >
 <item>
  <milk>semi</milk>
  <size>large</size>
  <drink>cappuccino</drink>
 </item>
 <location>takeAway</location>
 <cost>2. 0</cost>
 <status>ready</status>
 <link uri=″http://restbucks.com/receipt/1234″ rel=″receipt″/>
</order>
DELETE Receipt Response
<order xmlns=″http://schemas.restbucks.com″ >
 <item>
  <milk>semi</milk>
  <size>large</size>
  <drink>cappuccino</drink>
 </item>
 <location>takeAway</location>
 <cost>2. 0</cost>
 <status>taken</status>
</order>
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
Links
<order xmlns=″http://schemas.restbucks.com″ >
 <item>
  <milk>semi</milk>
  <size>large</size>
  <drink>cappuccino</drink>
 </item>
 <location>takeAway</location>
 <cost>2. 0</cost>
 <status>unpaid</status>
 <link uri=″http://restbucks.com/order/1234″ rel=″cancel″/>
 <link uri=″http://restbucks.com/payment/1234″ rel=″payment″/>
 <link uri=″http://restbucks.com/order/1234″ rel=″update″/>
 <link uri=″http://restbucks.com/order/1234″ rel=″self″/>
</order>
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″/>
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″/>
Better Links
<order xmlns=″http://schemas.restbucks.com″
 xmlns:dap=″http://schemas.restbucks.com/dap″>

 <dap:link mediaType=″application/vnd.restbucks+xml″
  uri=″http://restbucks.com/order/1234″
  rel=″http://relations.restbucks.com/cancel″/>

 <dap:link mediaType=″application/vnd.restbucks+xml″
  uri=″http://restbucks.com/payment/1234″
  rel=″http://relations.restbucks.com/payment″/>
More Benefits

Toggle availability of features

       Possibilities to change URLs

                       More flexible clients
Live Documentation
• Provide text/html representation as
  documentation
• Forms and links to interact with Service
• Great for exploring the API
What about WADL?
•   Static contracts
•   Nothing about ordering of interaction
•   Intention is tooling
•   Might be useful for CRUD Services
Evolving
Interfaces
Evolving Interfaces
Why?
• Impossible to predict the future
• All cannot jump simultaneously
Evolving Interfaces
Why?
• Impossible to predict the future
• All cannot jump simultaneously

Advice:
• You need a versioning strategy
• Know your consumers
• Be pragmatic
Types of Changes
•   2.1.3
•   major.minor.point
•   major = not compatible (new xml ns)
•   minor = compatible
•   point = no change to contract
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
Not Backwards Compatible
        Removing an operation

        Renaming an operation

Changing the parameters of an operation

 Changing the structure of a data type
Backwards Compatibility


                     Ignore
                     missing




 Ignore unknown
Forwards Compatibility


                     Ignore unknown




    Ignore missing
Optional Elements
                                     v1
                                                      v1


                                     v1
v1:
<xs:complexType name="address" >
      <xs:sequence>
             <xs:element name="street" type="xs:string"
                    minOccurs="1" maxOccurs="1"/>
            <xs:element name="country" type="xs:string"
                    minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
</xs:complexType>
Optional Elements
                                     v1
                                                      v2


                                     v2
v2:
<xs:complexType name="address" >
      <xs:sequence>
             <xs:element name="street" type="xs:string"
                    minOccurs="1" maxOccurs="1"/>
            <xs:element name="country" type="xs:string"
                    minOccurs="1" maxOccurs="1"/>
            <xs:element name="careOf" type="xs:string"
                    minOccurs="0" maxOccurs="1"/>
      </xs:sequence>
</xs:complexType>
##any                              v1
                                                      v1


                                     v1
v1:
<xs:complexType name="customer" >
      <xs:sequence>
             <xs:element name="name" type="xs:string" />
             <xs:element name="address" type="address"
                minOccurs="0" maxOccurs="unbounded"/>
             <xs:element name="eov1"/>
             <xs:any namespace="##any" processContents="lax"
                    minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
##any                              v1
                                                      v2


                                     v2
v2:
<xs:complexType name="customer" >
      <xs:sequence>
             <xs:element name="name" type="xs:string" />
             <xs:element name="address" type="address"
                minOccurs="0" maxOccurs="unbounded"/>
             <xs:element name="eov1"/>
             <xs:element name="vip" type="xs:boolean" />
             <xs:element name="eov2"/>
             <xs:any namespace="##any" processContents="lax"
                    minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
xs:extension
<xs:complexType name="instrument" abstract="true">

<xs:complexType name="stock" >
      <xs:complexContent>
            <xs:extension base="instrument">




    v1
                    v2


    v1
xs:extension
<xs:complexType name="instrument" abstract="true">

<xs:complexType name="stock" >
      <xs:complexContent>
            <xs:extension base="instrument">

<xs:complexType name="bond" >
      <xs:complexContent>
            <xs:extension base="instrument">


    v1
                    v2


    v2
Versioned Namespace

<types>
    <schema targetNamespace=
       "http://example.com/2003/10/15/stockquote.xsd"
        xmlns="http://www.w3.org/2000/10/XMLSchema">
      <element name="TradePriceRequest">
        <complexType>
          <all>
            <element name="tickerSymbol" type="string"/>
          </all>
        </complexType>
      </element>
      <element name="TradePriceResponse">
        <complexType>
Transformation

 v1
      T

           v2


 v2
Versioning of RESTful
      Services
Versioning Example
V 1.0

<account>
 <name>Inigo Montoya</name>
</account>
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
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
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
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
Vendor MIME type


• application/vnd.mycompany.myapp+xml
• Accept header of request
• Content-Type header of response
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
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
Scaling
 Out
GET to Update

GET /updateuser?name=Robert&newname=Bob

                          D !
                        BA
                  D   !
               BA
           D !
       B A
HTTP Caching
GET /foo HTTP/1.1
Host:
www.foo.com
User‐Agent:
FooBrowser/1.0
If‐Modified‐Since:
Mon,
01
Jan
1979
...
If‐None‐Match:
abcdef0123456789
Accept:
*



                        HTTP/1.1 200 OK
                        Content‐Type: text/html
                        Content‐Length: 24
                        Cache‐Control: public,
max‐age=300

                        Last‐Modified: Tue,
02
Jan
1979
...
                        ETag: abcdef0123456789
                        Vary: Accept
Alice   Gateway Cache   Backend
Alice                   Gateway Cache   Backend




        GET
/foo
        Host:
foo.com
Alice                   Gateway Cache             Backend




        GET
/foo                  GET
/foo
        Host:
foo.com             Host:
foo.com
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
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
Bob   (30 seconds later)   Gateway Cache   Backend
Bob   (30 seconds later)   Gateway Cache   Backend




      GET
/foo
      Host:
foo.com
Bob   (30 seconds later)                    Gateway Cache   Backend




      GET
/foo
      Host:
foo.com




            200
OK
            Cache‐Control:
public,
max‐age=60
            ETag:
abcdef012345
            Age:
30

            Hello
World
Carol   (60 seconds later)   Gateway Cache   Backend
Carol   (60 seconds later)   Gateway Cache   Backend




        GET
/foo
        Host:
foo.com
Carol   (60 seconds later)   Gateway Cache                           Backend




                                       GET
/foo
        GET
/foo                       Host:
foo.com
        Host:
foo.com                  If‐None‐Match:
abcdef012345
Carol   (60 seconds later)   Gateway Cache                                         Backend




                                       GET
/foo
        GET
/foo                       Host:
foo.com
        Host:
foo.com                  If‐None‐Match:
abcdef012345




                                               304
Not
Modified
                                               Cache‐Control:
public,
max‐age=60
Carol   (60 seconds later)                    Gateway Cache                                         Backend




                                                        GET
/foo
        GET
/foo                                        Host:
foo.com
        Host:
foo.com                                   If‐None‐Match:
abcdef012345




              200
OK                                            304
Not
Modified
              Cache‐Control:
public,
max‐age=60                 Cache‐Control:
public,
max‐age=60
              ETag:
abcdef012345

              Hello
World
HTTPS Scalability Drawbacks


       Cryptography costs

        Prevents caching
Other
Challenges
Contract first vs last

More up-front work
Need to learn all XML stuff

                        Decoupling
                     Better control
Validation and Errors


• Schema validation
• SOAP Fault
• Validation errors part of response
  structure
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
Concurrency

HTTP/1.1 201 Created
...
ETag: "44bd59eeb984c"


PUT /orders/123 HTTP/1.1
...
If‐Match: "44bd59eeb984c"


412 Precondition Failed
Data Mapping
Do I need mapping framework?

  Domain Objects != DTO

                          Tools
                            JAXB
                          Castor
                        XMLBeans
                             JiBX
                         XStream
Level of Detail


• Different consumers might need different
  level of detail
• Different services?
• Include spec in request
Associations


• In REST you include links to associated
  resources
• In practice you might need to embed
  related data
Service Composition


• General vs specific services?
• Canonical Data Model?
Transactions


• WS-Transaction
• Stay out of distributed transactions
Interoperability


• WS-Interoperability
• WS-I test tools
Security


•   HTTPS - transport level
•   WS-Security
•   Identify and authenticate the client
•   Integrity of the message
•   Safe from eavesdropping
Event-Driven Architecture

• Don’t use request-response for
  everything
• Asynchronous messaging is often a
  better alternative
Real World

• StreamFlow
• Amazon S3
 http://docs.amazonwebservices.com/AmazonS3/latest/API/

• eBay
 http://developer.ebay.com/support/docs/
 http://developer.ebay.com/webservices/latest/eBaySvc.wsdl
{
"commands":[],
"contexts":
["services","users","cases
","organizations","surface
"],
"index":null,
"queries":[]
}
Questions?




Patrik Nordwall
Thanks for listening!




Patrik Nordwall

Weitere ähnliche Inhalte

Was ist angesagt?

Html5 and web technology update
Html5 and web technology updateHtml5 and web technology update
Html5 and web technology updateDoug Domeny
 
Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Michael Girouard
 
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPAShaun Smith
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSCarol McDonald
 
Even faster django
Even faster djangoEven faster django
Even faster djangoGage Tseng
 
Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Brian Cavalier
 
Stop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalStop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalBjörn Brala
 
Introducing RaveJS: Spring Boot concepts for JavaScript applications
Introducing RaveJS: Spring Boot concepts for JavaScript applicationsIntroducing RaveJS: Spring Boot concepts for JavaScript applications
Introducing RaveJS: Spring Boot concepts for JavaScript applicationsJohn Hann
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Railsrfischer20
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scalab0ris_1
 
Introduction to RESTful Web Services
Introduction to RESTful Web ServicesIntroduction to RESTful Web Services
Introduction to RESTful Web ServicesFelipe Dornelas
 
Making Java REST with JAX-RS 2.0
Making Java REST with JAX-RS 2.0Making Java REST with JAX-RS 2.0
Making Java REST with JAX-RS 2.0Dmytro Chyzhykov
 
Alfresco tech talk live public api episode 64
Alfresco tech talk live public api episode 64Alfresco tech talk live public api episode 64
Alfresco tech talk live public api episode 64Alfresco Software
 
Neo4j: Import and Data Modelling
Neo4j: Import and Data ModellingNeo4j: Import and Data Modelling
Neo4j: Import and Data ModellingNeo4j
 
Android App Development 06 : Network &amp; Web Services
Android App Development 06 : Network &amp; Web ServicesAndroid App Development 06 : Network &amp; Web Services
Android App Development 06 : Network &amp; Web ServicesAnuchit Chalothorn
 
Webservices
WebservicesWebservices
Webservicess4al_com
 

Was ist angesagt? (20)

Web services tutorial
Web services tutorialWeb services tutorial
Web services tutorial
 
Html5 and web technology update
Html5 and web technology updateHtml5 and web technology update
Html5 and web technology update
 
Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5
 
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPA
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
Develop webservice in PHP
Develop webservice in PHPDevelop webservice in PHP
Develop webservice in PHP
 
Even faster django
Even faster djangoEven faster django
Even faster django
 
Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014
 
Stop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalStop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in Drupal
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Introducing RaveJS: Spring Boot concepts for JavaScript applications
Introducing RaveJS: Spring Boot concepts for JavaScript applicationsIntroducing RaveJS: Spring Boot concepts for JavaScript applications
Introducing RaveJS: Spring Boot concepts for JavaScript applications
 
OAuth: Trust Issues
OAuth: Trust IssuesOAuth: Trust Issues
OAuth: Trust Issues
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scala
 
Introduction to RESTful Web Services
Introduction to RESTful Web ServicesIntroduction to RESTful Web Services
Introduction to RESTful Web Services
 
Making Java REST with JAX-RS 2.0
Making Java REST with JAX-RS 2.0Making Java REST with JAX-RS 2.0
Making Java REST with JAX-RS 2.0
 
Alfresco tech talk live public api episode 64
Alfresco tech talk live public api episode 64Alfresco tech talk live public api episode 64
Alfresco tech talk live public api episode 64
 
Neo4j: Import and Data Modelling
Neo4j: Import and Data ModellingNeo4j: Import and Data Modelling
Neo4j: Import and Data Modelling
 
Android App Development 06 : Network &amp; Web Services
Android App Development 06 : Network &amp; Web ServicesAndroid App Development 06 : Network &amp; Web Services
Android App Development 06 : Network &amp; Web Services
 
Webservices
WebservicesWebservices
Webservices
 

Andere mochten auch (9)

On being RESTful
On being RESTfulOn being RESTful
On being RESTful
 
REST Fundamentals
REST FundamentalsREST Fundamentals
REST Fundamentals
 
Pragmatic Rest
Pragmatic RestPragmatic Rest
Pragmatic Rest
 
REST
RESTREST
REST
 
REST Methodologies
REST MethodologiesREST Methodologies
REST Methodologies
 
REST overview
REST overviewREST overview
REST overview
 
REpresentational State Transfer
REpresentational State TransferREpresentational State Transfer
REpresentational State Transfer
 
REST presentation
REST presentationREST presentation
REST presentation
 
Doing REST Right
Doing REST RightDoing REST Right
Doing REST Right
 

Ähnlich wie Ws rest

Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jerseyb_kathir
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsCarol McDonald
 
Couchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problemCouchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problemdelagoya
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to TornadoGavin Roy
 
Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2kriszyp
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011Shreedhar Ganapathy
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Solutions for bi-directional Integration between Oracle RDMBS & Apache Kafka
Solutions for bi-directional Integration between Oracle RDMBS & Apache KafkaSolutions for bi-directional Integration between Oracle RDMBS & Apache Kafka
Solutions for bi-directional Integration between Oracle RDMBS & Apache KafkaGuido Schmutz
 
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...confluent
 
Solutions for bi-directional integration between Oracle RDBMS & Apache Kafka
Solutions for bi-directional integration between Oracle RDBMS & Apache KafkaSolutions for bi-directional integration between Oracle RDBMS & Apache Kafka
Solutions for bi-directional integration between Oracle RDBMS & Apache KafkaGuido Schmutz
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)Kashif Imran
 
Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Sven Efftinge
 
RESTful services
RESTful servicesRESTful services
RESTful servicesgouthamrv
 

Ähnlich wie Ws rest (20)

Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
 
Rest
RestRest
Rest
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
 
Couchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problemCouchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problem
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2
 
Web Services
Web ServicesWeb Services
Web Services
 
Rest web services
Rest web servicesRest web services
Rest web services
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Solutions for bi-directional Integration between Oracle RDMBS & Apache Kafka
Solutions for bi-directional Integration between Oracle RDMBS & Apache KafkaSolutions for bi-directional Integration between Oracle RDMBS & Apache Kafka
Solutions for bi-directional Integration between Oracle RDMBS & Apache Kafka
 
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...
Solutions for bi-directional integration between Oracle RDBMS and Apache Kafk...
 
Solutions for bi-directional integration between Oracle RDBMS & Apache Kafka
Solutions for bi-directional integration between Oracle RDBMS & Apache KafkaSolutions for bi-directional integration between Oracle RDBMS & Apache Kafka
Solutions for bi-directional integration between Oracle RDBMS & Apache Kafka
 
08 ajax
08 ajax08 ajax
08 ajax
 
SOAP-based Web Services
SOAP-based Web ServicesSOAP-based Web Services
SOAP-based Web Services
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)
 
Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)Domain Specific Languages (EclipseCon 2012)
Domain Specific Languages (EclipseCon 2012)
 
Ntg web services
Ntg   web servicesNtg   web services
Ntg web services
 
Web service introduction
Web service introductionWeb service introduction
Web service introduction
 
RESTful services
RESTful servicesRESTful services
RESTful services
 

Ws rest

  • 1. Web Services and REST Patrik Nordwall
  • 2. Roadmap Introduction to Web Services Introduction to REST Design Challenges
  • 3. Introduction to Web Services Patrik Nordwall
  • 4. Outline Concepts (SOAP, WSDL, ...) XML Schema Demo
  • 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
  • 7. SOAP • Simple Object Access Protocol • Service Invocation • Cross-platform remote calls • Usually XML over HTTP (POST)
  • 9. SOAP Example - request <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/ <soap:Header> </soap:Header> <soap:Body> <m:GetStockPrice xmlns:m="http://www.example.o <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope>
  • 10. SOAP Example - response <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelop soap:encodingStyle="http://www.w3.org/2001/12/soap <soap:Body xmlns:m="http://www.example.org/stock">   <m:GetStockPriceResponse>     <m:Price>34.5</m:Price>   </m:GetStockPriceResponse> </soap:Body> </soap:Envelope>
  • 11. WSDL ‘wiz-del’ • Web Service Description Language • Machine-readable language For tools - not humans
  • 12. WSDL Structure • portType - operations • messages • types • binding - communication protocols
  • 13. WSDL Example <message name="getTermRequest">   <part name="term" type="xs:string"/> </message> <message name="getTermResponse">   <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms">   <operation name="getTerm">     <input message="getTermRequest"/>     <output message="getTermResponse"/>   </operation> </portType>
  • 14. WSDL Binding <binding type="glossaryTerms" name="b1">    <soap:binding style="document"    transport="http://schemas.xmlsoap.org/soap/http    <operation>      <soap:operation soapAction="http://example.co      <input><soap:body use="literal"/></input>      <output><soap:body use="literal"/></output>   </operation> </binding>
  • 15. UDDI • Universal Discovery and Directory Interface • Kind of yellow pages • You probably don’t need all UDDI features
  • 16. XML Schema Data definition in XML format Important for Service Contract
  • 17. XML Example <?xml version="1.0"?> <note>   <to>Tove</to>   <from>Jani</from>   <heading>Reminder</heading>   <body>Don't forget me this weekend!</body> </note>
  • 18. XSD Example <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="note">   <xs:complexType>     <xs:sequence>       <xs:element name="to" type="xs:string"/>       <xs:element name="from" type="xs:string"/>       <xs:element name="heading" type="xs:string"/>       <xs:element name="body" type="xs:string"/>     </xs:sequence>   </xs:complexType> </xs:element> </xs:schema>
  • 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>
  • 20. XSD Types <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/>
  • 21. XSD Types <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> • xs:string • xs:decimal • xs:integer • xs:boolean • xs:date • xs:time
  • 22. XSD Restriction <xs:element name="age" type="Age"/> <xs:simpleType name="Age"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType>
  • 23. XSD Optional/Required <xs:attribute name="lang" type="xs:string" use="required"/> <xs:element name="body" type="xs:string" minOccurs="0"/> <xs:element name="body" type="xs:string" nillable="true"/>
  • 24. XSD Default Values <xs:attribute name="lang" type="xs:string" default="EN"/>
  • 25. XSD Any Order <xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> </xs:element>
  • 26. XSD Choice <xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> </xs:element>
  • 27. XSD Extension <xs:element name="employee" type="fullpersoninfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="fullpersoninfo"> <xs:complexContent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
  • 28. WS Tools • Apache CXF • Axis 2 • Spring Web Services • Metro (GlassFish) • WebLogic (wsdlc) • JBossWS • ...
  • 32. REST is NOT Architecture Protocol Technology
  • 33. REST is an architectural style
  • 34. REST • Representational State Transfer • 'RESTful' == Conforming to the REST constraints
  • 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
  • 39. Stateless Stateless communication State can live in resource state State can live in client
  • 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
  • 42. JSON Objects {} & Lists []
  • 43. JSON { "name" : "Patrik", "age" : 37, "parent" : true }
  • 44. JSON [ { "title": "Perpendicular", "artist": "Deep Purple" }, { "title": "Dover Calais", "artist" : "Style" } ]
  • 45. JSON { "name" : "Patrik", "children" : [ "Ebba", "Klara" ] }
  • 46. REST Tools Jersey Apache CXF Restlet RestEasy Spring 3.0 Restfulie
  • 48. Design Challenges of Web Services and RESTful Web Services Patrik Nordwall
  • 49. Outline Hypermedia Evolving interfaces Scaling out Other Challenges
  • 51. Hypermedia Link things together One entry point Links declare transitions to next step Don’t need a static contract description
  • 52. HATEOAS Hypermedia As The Engine Of Application State
  • 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
  • 62. POST Order <order xmlns=″http://schemas.restbucks.com″ > <item> <milk>semi</milk> <size>large</size> <drink>cappuccino</drink> </item> <location>takeAway</location> </order>
  • 63. Order Response <order xmlns=″http://schemas.restbucks.com″ > <item> <milk>semi</milk> <size>large</size> <drink>cappuccino</drink> </item> <location>takeAway</location> <cost>2. 0</cost> <status>unpaid</status> <link uri=″http://restbucks.com/order/1234″ rel=″cancel″/> <link uri=″http://restbucks.com/payment/1234″ rel=″payment″/> <link uri=″http://restbucks.com/order/1234″ rel=″update″/> <link uri=″http://restbucks.com/order/1234″ rel=″self″/> </order>
  • 64. PUT Payment <payment xmlns=″http://schemas.restbucks.com″> <amount>2. 0</amount> <cardholderName>Michael Faraday</cardholderName> <cardNumber>11223344</cardNumber> <expiryMonth>12</expiryMonth> <expiryYear>12</expiryYear> </payment>
  • 65. Payment Response <payment xmlns=″http://schemas.restbucks.com″> <amount>2. 0</amount> <cardholderName>Michael Faraday</cardholderName> <cardNumber>11223344</cardNumber> <expiryMonth>12</expiryMonth> <expiryYear>12</expiryYear> <link uri=″http://restbucks.com/order/1234″ rel=″order″/> <link uri=″http: //restbucks.com/receipt/1234″ rel=″receipt″/> </payment>
  • 66. GET Order Response <order xmlns=″http://schemas.restbucks.com″ > <item> <milk>semi</milk> <size>large</size> <drink>cappuccino</drink> </item> <location>takeAway</location> <cost>2. 0</cost> <status>preparing</status> <link uri=″http://restbucks.com/order/1234″ rel=″self″/> </order>
  • 67. ...GET Order Response <order xmlns=″http://schemas.restbucks.com″ > <item> <milk>semi</milk> <size>large</size> <drink>cappuccino</drink> </item> <location>takeAway</location> <cost>2. 0</cost> <status>ready</status> <link uri=″http://restbucks.com/receipt/1234″ rel=″receipt″/> </order>
  • 68. DELETE Receipt Response <order xmlns=″http://schemas.restbucks.com″ > <item> <milk>semi</milk> <size>large</size> <drink>cappuccino</drink> </item> <location>takeAway</location> <cost>2. 0</cost> <status>taken</status> </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
  • 70. Links <order xmlns=″http://schemas.restbucks.com″ > <item> <milk>semi</milk> <size>large</size> <drink>cappuccino</drink> </item> <location>takeAway</location> <cost>2. 0</cost> <status>unpaid</status> <link uri=″http://restbucks.com/order/1234″ rel=″cancel″/> <link uri=″http://restbucks.com/payment/1234″ rel=″payment″/> <link uri=″http://restbucks.com/order/1234″ rel=″update″/> <link uri=″http://restbucks.com/order/1234″ rel=″self″/> </order>
  • 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″/>
  • 73. Better Links <order xmlns=″http://schemas.restbucks.com″ xmlns:dap=″http://schemas.restbucks.com/dap″> <dap:link mediaType=″application/vnd.restbucks+xml″ uri=″http://restbucks.com/order/1234″ rel=″http://relations.restbucks.com/cancel″/> <dap:link mediaType=″application/vnd.restbucks+xml″ uri=″http://restbucks.com/payment/1234″ rel=″http://relations.restbucks.com/payment″/>
  • 74. More Benefits Toggle availability of features Possibilities to change URLs More flexible clients
  • 75. Live Documentation • Provide text/html representation as documentation • Forms and links to interact with Service • Great for exploring the API
  • 76. What about WADL? • Static contracts • Nothing about ordering of interaction • Intention is tooling • Might be useful for CRUD Services
  • 78. Evolving Interfaces Why? • Impossible to predict the future • All cannot jump simultaneously
  • 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
  • 83. Backwards Compatibility Ignore missing Ignore unknown
  • 84. Forwards Compatibility Ignore unknown Ignore missing
  • 85. Optional Elements v1 v1 v1 v1: <xs:complexType name="address" > <xs:sequence> <xs:element name="street" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="country" type="xs:string" minOccurs="1" maxOccurs="1"/> </xs:sequence> </xs:complexType>
  • 86. Optional Elements v1 v2 v2 v2: <xs:complexType name="address" > <xs:sequence> <xs:element name="street" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="country" type="xs:string" minOccurs="1" maxOccurs="1"/> <xs:element name="careOf" type="xs:string" minOccurs="0" maxOccurs="1"/> </xs:sequence> </xs:complexType>
  • 87. ##any v1 v1 v1 v1: <xs:complexType name="customer" > <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="address" type="address" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="eov1"/> <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> <xs:attribute name="id" type="xs:string" use="required"/> </xs:complexType>
  • 88. ##any v1 v2 v2 v2: <xs:complexType name="customer" > <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="address" type="address" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="eov1"/> <xs:element name="vip" type="xs:boolean" /> <xs:element name="eov2"/> <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> <xs:attribute name="id" type="xs:string" use="required"/> </xs:complexType>
  • 89. xs:extension <xs:complexType name="instrument" abstract="true"> <xs:complexType name="stock" > <xs:complexContent> <xs:extension base="instrument"> v1 v2 v1
  • 90. xs:extension <xs:complexType name="instrument" abstract="true"> <xs:complexType name="stock" > <xs:complexContent> <xs:extension base="instrument"> <xs:complexType name="bond" > <xs:complexContent> <xs:extension base="instrument"> v1 v2 v2
  • 91. Versioned Namespace <types> <schema targetNamespace= "http://example.com/2003/10/15/stockquote.xsd" xmlns="http://www.w3.org/2000/10/XMLSchema"> <element name="TradePriceRequest"> <complexType> <all> <element name="tickerSymbol" type="string"/> </all> </complexType> </element> <element name="TradePriceResponse"> <complexType>
  • 92. Transformation v1 T v2 v2
  • 94. Versioning Example V 1.0 <account> <name>Inigo Montoya</name> </account>
  • 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
  • 103. GET to Update GET /updateuser?name=Robert&newname=Bob D ! BA D ! BA D ! B A
  • 104. HTTP Caching GET /foo HTTP/1.1 Host:
www.foo.com User‐Agent:
FooBrowser/1.0 If‐Modified‐Since:
Mon,
01
Jan
1979
... If‐None‐Match:
abcdef0123456789 Accept:
* HTTP/1.1 200 OK Content‐Type: text/html Content‐Length: 24 Cache‐Control: public,
max‐age=300
 Last‐Modified: Tue,
02
Jan
1979
... ETag: abcdef0123456789 Vary: Accept
  • 105. Alice Gateway Cache Backend
  • 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
  • 110. Bob (30 seconds later) Gateway Cache Backend
  • 111. Bob (30 seconds later) Gateway Cache Backend GET
/foo Host:
foo.com
  • 112. Bob (30 seconds later) Gateway Cache Backend GET
/foo Host:
foo.com 200
OK Cache‐Control:
public,
max‐age=60 ETag:
abcdef012345 Age:
30 Hello
World
  • 113. Carol (60 seconds later) Gateway Cache Backend
  • 114. Carol (60 seconds later) Gateway Cache Backend GET
/foo Host:
foo.com
  • 115. Carol (60 seconds later) Gateway Cache Backend GET
/foo GET
/foo Host:
foo.com Host:
foo.com If‐None‐Match:
abcdef012345
  • 116. Carol (60 seconds later) Gateway Cache Backend GET
/foo GET
/foo Host:
foo.com Host:
foo.com If‐None‐Match:
abcdef012345 304
Not
Modified Cache‐Control:
public,
max‐age=60
  • 117. Carol (60 seconds later) Gateway Cache Backend GET
/foo GET
/foo Host:
foo.com Host:
foo.com If‐None‐Match:
abcdef012345 200
OK 304
Not
Modified Cache‐Control:
public,
max‐age=60 Cache‐Control:
public,
max‐age=60 ETag:
abcdef012345 Hello
World
  • 118. HTTPS Scalability Drawbacks Cryptography costs Prevents caching
  • 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
  • 124. Data Mapping Do I need mapping framework? Domain Objects != DTO Tools JAXB Castor XMLBeans JiBX XStream
  • 125. Level of Detail • Different consumers might need different level of detail • Different services? • Include spec in request
  • 126. Associations • In REST you include links to associated resources • In practice you might need to embed related data
  • 127. Service Composition • General vs specific services? • Canonical Data Model?
  • 128. Transactions • WS-Transaction • Stay out of distributed transactions
  • 130. Security • HTTPS - transport level • WS-Security • Identify and authenticate the client • Integrity of the message • Safe from eavesdropping
  • 131. Event-Driven Architecture • Don’t use request-response for everything • Asynchronous messaging is often a better alternative
  • 132. Real World • StreamFlow • Amazon S3 http://docs.amazonwebservices.com/AmazonS3/latest/API/ • eBay http://developer.ebay.com/support/docs/ http://developer.ebay.com/webservices/latest/eBaySvc.wsdl
  • 133.