Click here to load reader

RESTful Web Services with JAX-RS

  • View
    13.616

  • Download
    0

Embed Size (px)

Text of RESTful Web Services with JAX-RS

  1. 1. REST Carol McDonald, Java Architect
  2. 2. Agenda
    • REST Primer
    • RESTful Design and API Elements
    • Building a Simple Service
    • Status
    • Q & A
  3. 3. REpresentational State Transfer
    • Get
    • http://www.depot.com/parts
    Response XML data = REpresentational State Transfer
        • TheURLidentifies theresource
        • HTTP GET methodoperates on the resource
        • htmlpage is transferredto the browser
          • REpresentational State transfer
        • The page ( application state ) is stored on theclient(browser)
        • Click on the link (resource) in page ( hypermedia )
          • New state transfer occurs
  4. 4. REST architecture for the WebWeb platforms Addressable Resources Web Container Addressable Resources Web Container
  5. 5. REST Tenets
    • Resources ( nouns )
      • Identifiedby aURI , For example:
        • http://www.parts-depot.com/parts
    • Methods ( verbs )to manipulate the nouns
      • Small fixed set:
        • GET, PUT, POST, DELETE
          • Read, Update, Create, Delete
    • Representationof the Resource
      • data and statetransferred between client and server
      • XML, JSON ...
    • Useverbstoexchangeapplication state andrepresentation
  6. 6. HTTP Example Request GET /music/artists/beatles/recordingsHTTP/1.1 Host: media.example.com Accept: application/xml Response HTTP/1.1 200 OK Date: Tue, 08 May 2007 16:41:58 GMT Server: Apache/1.3.6 Content-Type: application/xml; charset=UTF-8 Method ResourceRepresentation State transfer
  7. 7. REST in Five Steps*
    • Give everything an ID
    • Use standard methods
    • Link things together
    • Multiple representations
    • Stateless communications
      • http://www.infoq.com/articles/rest-introduction
    Uniform Interface
  8. 8. Give Every Thing an Idhttp://company.com/customers/123456 Resource Collection name Primary key http://company.com/customers/123456/orders/12 http://example.com/orders/2007/11 http://example.com/products?color=green URI is the id, Every resource has a URI
    • URIs identify :
      • items, collections of items, virtual and physical objects, or computation results.
  9. 9. REST in Five Steps*
    • Give everything an ID
    • Use standard methods
    • Link things together
    • Multiple representations
    • Stateless communications
      • http://www.infoq.com/articles/rest-introduction
    Uniform Interface
  10. 10. Use StandardHTTP Methods: GET= Read
    • GET to retrieve information
      • Shouldnot modifyanything
      • Cacheable
      • Example
        • GET /store/customers/123456
  11. 11. Use StandardHTTP Methods: POST= Create
    • POST toadd newinformation
    • add the entity ,appendto the container resource
        • POST /store/customers
        • carol
        • Response
        • HTTP/1.1 201 Created
        • Location: http://store/customers/789
  12. 12. Use StandardHTTP Methods: PUT = Update
    • PUT toupdateinformation
    • Full entitycreate/replaceused when you know the id
      • Example
        • PUT /store/customers/123456
        • jane
  13. 13. Use StandardHTTP Methods: DELETE
    • Remove (logical) an entity
      • Example
        • DELETE /store/customers/123456
  14. 14. Use StandardMethods:
      • http://www.infoq.com/articles/rest-introduction
    Order Customer Mgmt Example
  15. 15. Use StandardMethods:
    • /orders
      • GET - list all orders
      • POST - submit a new order
    • /orders/{order-id}
      • GET - get an order representation
      • PUT - update an order
      • DELETE - cancel an order
    • /orders/average-sale
      • GET - calculate average sale
    • /customers
      • GET - list all customers
      • POST - create a new customer
    • /customers/{cust-id}
      • GET - get a customer representation
      • DELETE- remove a customer
    • /customers/{cust-id}/orders
      • GET - get the orders of a customer
    Order Customer Mgmt Example
      • http://www.infoq.com/articles/rest-introduction
  16. 16. Use StandardHTTP Methods
    • HTTP Get, Head
      • Shouldnot modifyanything
      • Cache-able
        • With Correct use ofLast-ModifiedandETag
    • Idempotency:
      • PUT, DELETE, GET, HEAD can berepeatedand theresults are the same
  17. 17. REST in Five Steps*
    • Give everything an ID
    • Use standard methods
    • Link things together
    • Multiple representations
    • Stateless communications * Inspired by Stefan Tilkov:http://www.innoq.com/blog/st/presentations/2008/2008-03-13-REST-Intro--QCon-London.pdf
  18. 18. Link Things Together
    • Representations contain links to other resources
    • Service provideslinks in responseto the Client
      • Enables client to move the application from onestateto thenextby following alink
      • Representationscontain links to otherresources:
  19. 19. Link Things Together
    • Representationscontainlinksto otherresources
    • Put a service in different states by following links and filling in forms
      • Hypermedia as the engine of application state
      • HTML, XML, JSON, N3 can all be Hypermedia
    • Client is in charge
      • Serviceofferspotentialstates
      • Service does not impose state transition order
  20. 20. REST in Five Steps*
    • Give everything an ID
    • Use standard methods
    • Link things together
    • Multiple representations
    • Stateless communications * Inspired by Stefan Tilkov:http://www.innoq.com/blog/st/presentations/2008/2008-03-13-REST-Intro--QCon-London.pdf
  21. 21. Multiple Representations
    • Offer data in a variety of formats, for different needs
      • XML
      • JSON
      • (X)HTML
    • Supportcontent negotiation
      • Accept header GET /foo Accept:application/json
      • URI-based GET / foo.json
  22. 22.
      • content negotiation
    Multiple Representations Content-type HTTP header Accept HTTP header Request GET /music/artists/beatles/recordingsHTTP/1.1 Host: media.example.com Accept : application/xml Response HTTP/1.1 200 OK Date: Tue, 08 May 2007 16:41:58 GMT Server: Apache/1.3.6 Content-Type : application/xml; charset=UTF-8 FormatRepresentation
  23. 23. REST in Five Steps*
    • Give everything an ID
    • Use standard methods
    • Link things together
    • Multiple representations
    • Stateless communications * Inspired by Stefan Tilkov:http://www.innoq.com/blog/st/presentations/2008/2008-03-13-REST-Intro--QCon-London.pdf
  24. 24. Stateless Communications
    • HTTP protocol isstateless
    • Everythingrequired to processarequest contained intherequest
      • No client session on the server
      • Eliminatesmanyfailureconditions
    • applicationstate kept onClient
    • Service responsible forresourcestate
  25. 25. Common Patterns: Container, Item Server in control of URI
    • Container acollectionof items
    • List catalog items:GET /catalog/items
    • Additem to container:POST/catalog/items
      • with item in request
      • URI of item retur

Search related