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

Ws rest

  • 1.
    Web Services andREST Patrik Nordwall
  • 2.
    Roadmap Introduction to WebServices Introduction to REST Design Challenges
  • 3.
    Introduction to WebServices Patrik Nordwall
  • 4.
    Outline Concepts (SOAP, WSDL,...) XML Schema Demo
  • 5.
    What is aWeb Service? Application integration based on open standards (HTTP, XML) Published Interface Application functionality packaged as a single unit and exposed to the network
  • 6.
  • 7.
    SOAP • Simple Object Access Protocol • Service Invocation • Cross-platform remote calls • Usually XML over HTTP (POST)
  • 8.
  • 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 Discoveryand Directory Interface • Kind of yellow pages • You probably don’t need all UDDI features
  • 16.
    XML Schema Datadefinition 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:schemaxmlns: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:attributename="lang" type="xs:string" default="EN"/>
  • 25.
    XSD Any Order <xs:elementname="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 • ...
  • 29.
  • 30.
  • 31.
  • 32.
    REST is NOT Architecture Protocol Technology
  • 33.
    REST is anarchitectural style
  • 34.
    REST • Representational StateTransfer • '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
  • 36.
  • 37.
    Link Things Together <orderself='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 • Multiplerepresentations 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
  • 47.
  • 48.
    Design Challenges of Web Services and RESTful Web Services Patrik Nordwall
  • 49.
    Outline Hypermedia Evolving interfaces Scaling out Other Challenges
  • 50.
  • 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
  • 53.
  • 54.
  • 55.
    Restbucks GETa Cup Of Coffee Make selection Pay Wait for a while Collect drink
  • 56.
    Restbucks - NormalFlow Customer POST http://restbucks.com/order Restbucks 201 + order
  • 57.
    Restbucks - NormalFlow Customer POST http://restbucks.com/order Restbucks 201 + order PUT http://restbucks.com/payment/1234 201 + receipt
  • 58.
    Restbucks - NormalFlow 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 - NormalFlow 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 <orderxmlns=″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 <orderxmlns=″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 <orderxmlns=″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 availabilityof features Possibilities to change URLs More flexible clients
  • 75.
    Live Documentation • Providetext/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
  • 77.
  • 78.
    Evolving Interfaces Why? • Impossibleto predict the future • All cannot jump simultaneously
  • 79.
    Evolving Interfaces Why? • Impossibleto 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 • Nocompatibility • 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:complexTypename="stock" > <xs:complexContent> <xs:extension base="instrument"> v1 v2 v1
  • 90.
    xs:extension <xs:complexType name="instrument" abstract="true"> <xs:complexTypename="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.
  • 93.
  • 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 V1.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 V1.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
  • 102.
  • 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
  • 119.
  • 120.
    Contract first vslast 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
  • 123.
  • 124.
    Data Mapping Do Ineed 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 RESTyou include links to associated resources • In practice you might need to embed related data
  • 127.
    Service Composition • Generalvs specific services? • Canonical Data Model?
  • 128.
    Transactions • WS-Transaction • Stayout of distributed transactions
  • 129.
  • 130.
    Security • HTTPS - transport level • WS-Security • Identify and authenticate the client • Integrity of the message • Safe from eavesdropping
  • 131.
    Event-Driven Architecture • Don’tuse 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
  • 134.
  • 135.
  • 136.