Introduction to RESTfulWeb Services09/2012Wei Li
Agenda• RESTful in General• URI Design• Java Implementation• Others• Test• Demonstration
RESTful in General
In a Nutshell• RESTful is about resources• RESTful is about how to represent theresource in different ways• RESTful is abo...
Define REST• REST stands for Representational StateTransfer• An architecture style for designing networkedapplications
Define REST• REST offers a simple, interoperable, andflexible way of writing web services that canbe very different than t...
RESTful Is Not• A protocol• A standard• A replacement for SOAP
RESTful Architectural Principles• Addressable resources• A uniform, constrained interface• Representation oriented• Statel...
Resources• The key abstraction of information and data inREST• Each resource must be addressable via a URI(Uniform Resourc...
Addressability• Every object and resource in the systemshould be reachable through a uniqueidentifier• Managed through the...
Uniform and Constrained Interface• Don’t have an “action” parameter in URI• Use only the methods of HTTP for the webservic...
Representation Oriented• The user interacts with services usingrepresentations of that service• A resource referenced by o...
Stateless Communication• No client session data stored on the server• If there are needs for session-specific data, itshou...
RESTful and HTTP• REST isnt protocol specific• However when talking about REST, peopleusually mean REST over HTTP• Benefit...
REST TriangleSource: http://en.wikipedia.org/wiki/File:Resttriangle.svg
HTTP Methods• GET• PUT• DELETE• POST• HEAD• OPTIONS
CRUD Operation Mappedto HTTP Methods in RESTfulOPERATION HTTP METHODCreate POSTRead GETUpdate PUTDelete DELETE
HTTP Response Code• 200 OK• 201 Created• 202 Accepted• 204 Success• 301 Moved Permanently• 302 Found• 303 See Other• 304 N...
HTTP Response Code• 401 Unauthorized• 403 Forbidden• 404 Not Found• 405 Method Not Allowed• 409 Conflict• 411 Length Requi...
Create a ResourceSource: Rest in Practice
Read a ResourceSource: Rest in Practice
Update a ResourceSource: Rest in Practice
Delete a ResourceSource: Rest in Practice
URI Design
URI• Human meaningful• Hierarchical• Nouns• No verbs
URI Examples• http://localhost:9999/restapi/books– GET – get all books– POST – add a new book• http://localhost:9999/resta...
URI Examples• Twitter REST API v1.1 example:– https://dev.twitter.com/docs/api/1.1
Content Negotiation
Content Negotiation and URI• http://localhost:9999/restapi/books/{id}.xml• http://localhost:9999/restapi/books/{id}.json• ...
Content Negotiation and URI• https://cwiki.apache.org/WINK/jax-rs-content-negotiation.html
Java Implementation
JSR-311• POJO based• HTTP centric• Format independence• Container independence• Inclusion in Java EE
JSR-311 Annotations
JSR 311 Annotations
Available Java Frameworks• Jersey• CXF• Wink• RESTEasy
Others
WADL• The Web Application DescriptionLanguage• A machine-readable XML description ofHTTP-based web applications (typically...
WADL• Example (using Jersey implementation)http://localhost:9999/restapi/application.wadl
Versioning• Put the version in URIhttp://localhost:9999/restapi/v2.0/books
Versioning• put the version in the Accept Header ofthe requesthttp://stackoverflow.com/questions/389169/best-practices-for...
Security• HTTPS (encrypt channel)• Web ACL• Others (more advanced)
Error Handling• Use standard error message format asresponse• Use Non-200 response codesHTTP/1.1 400 Bad RequestContent-Ty...
Demonstration• A simple one page web applicationhttp://mybooks.cloudfoundry.com/books.html• Restful web service is impleme...
Test
Test RESTful Web Services• Using cURL
Test RESTful Web Services• Using cURL
Test RESTful Web Services• Using cURL
Test RESTful Web Services• Using SOAPUI
Test RESTful Web Services• Using SOAPUI
Test RESTful Web Services• Using SOAPUI
Test RESTful Web Services• Using SOAPUI
Test RESTful Web Services• Using SOAPUI
Reference• http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
Books
Upcoming SlideShare
Loading in …5
×

Introduction to Restful Web Services

1,879 views

Published on

An introduction to restful web services

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,879
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
43
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to Restful Web Services

  1. 1. Introduction to RESTfulWeb Services09/2012Wei Li
  2. 2. Agenda• RESTful in General• URI Design• Java Implementation• Others• Test• Demonstration
  3. 3. RESTful in General
  4. 4. In a Nutshell• RESTful is about resources• RESTful is about how to represent theresource in different ways• RESTful is about how to manipulate theresource
  5. 5. Define REST• REST stands for Representational StateTransfer• An architecture style for designing networkedapplications
  6. 6. Define REST• REST offers a simple, interoperable, andflexible way of writing web services that canbe very different than the RPC mechanismslike CORBA and WS-*
  7. 7. RESTful Is Not• A protocol• A standard• A replacement for SOAP
  8. 8. RESTful Architectural Principles• Addressable resources• A uniform, constrained interface• Representation oriented• Stateless communicate• Hypermedia As The Engine Of ApplicationState (HATEOAS)
  9. 9. Resources• The key abstraction of information and data inREST• Each resource must be addressable via a URI(Uniform Resource Identifier)• Everything can be a resource
  10. 10. Addressability• Every object and resource in the systemshould be reachable through a uniqueidentifier• Managed through the use of URIs
  11. 11. Uniform and Constrained Interface• Don’t have an “action” parameter in URI• Use only the methods of HTTP for the webservices• HTTP has a small fixed set of operationalmethods. Each method has a specific purposeand meaning
  12. 12. Representation Oriented• The user interacts with services usingrepresentations of that service• A resource referenced by one URI can havedifferent formats.– HTML (for browsers)– XML (for application)– JSON (for JavaScript)– Excel spreadsheet– Image
  13. 13. Stateless Communication• No client session data stored on the server• If there are needs for session-specific data, itshould be held and maintained by the clientand transferred to the server with eachrequest as needed• A service layer that does not have to maintainclient sessions is much easier to scale
  14. 14. RESTful and HTTP• REST isnt protocol specific• However when talking about REST, peopleusually mean REST over HTTP• Benefits of using HTTP for RESTful services: Familiarity Interoperability Interoperability
  15. 15. REST TriangleSource: http://en.wikipedia.org/wiki/File:Resttriangle.svg
  16. 16. HTTP Methods• GET• PUT• DELETE• POST• HEAD• OPTIONS
  17. 17. CRUD Operation Mappedto HTTP Methods in RESTfulOPERATION HTTP METHODCreate POSTRead GETUpdate PUTDelete DELETE
  18. 18. HTTP Response Code• 200 OK• 201 Created• 202 Accepted• 204 Success• 301 Moved Permanently• 302 Found• 303 See Other• 304 Not Modified• 500 Internal Error• 503 Service Unavailable
  19. 19. HTTP Response Code• 401 Unauthorized• 403 Forbidden• 404 Not Found• 405 Method Not Allowed• 409 Conflict• 411 Length Required• 413 Entity Too Long• 415 Unsupported Media Type
  20. 20. Create a ResourceSource: Rest in Practice
  21. 21. Read a ResourceSource: Rest in Practice
  22. 22. Update a ResourceSource: Rest in Practice
  23. 23. Delete a ResourceSource: Rest in Practice
  24. 24. URI Design
  25. 25. URI• Human meaningful• Hierarchical• Nouns• No verbs
  26. 26. URI Examples• http://localhost:9999/restapi/books– GET – get all books– POST – add a new book• http://localhost:9999/restapi/books/{id}– GET – get book whose id is provided– PUT – update the book whose id is provided– DELETE – delete the book whose is provided
  27. 27. URI Examples• Twitter REST API v1.1 example:– https://dev.twitter.com/docs/api/1.1
  28. 28. Content Negotiation
  29. 29. Content Negotiation and URI• http://localhost:9999/restapi/books/{id}.xml• http://localhost:9999/restapi/books/{id}.json• http://localhost:9999/restapi/books/{id}.pdf
  30. 30. Content Negotiation and URI• https://cwiki.apache.org/WINK/jax-rs-content-negotiation.html
  31. 31. Java Implementation
  32. 32. JSR-311• POJO based• HTTP centric• Format independence• Container independence• Inclusion in Java EE
  33. 33. JSR-311 Annotations
  34. 34. JSR 311 Annotations
  35. 35. Available Java Frameworks• Jersey• CXF• Wink• RESTEasy
  36. 36. Others
  37. 37. WADL• The Web Application DescriptionLanguage• A machine-readable XML description ofHTTP-based web applications (typicallyRESTful web services).
  38. 38. WADL• Example (using Jersey implementation)http://localhost:9999/restapi/application.wadl
  39. 39. Versioning• Put the version in URIhttp://localhost:9999/restapi/v2.0/books
  40. 40. Versioning• put the version in the Accept Header ofthe requesthttp://stackoverflow.com/questions/389169/best-practices-for-api-versioning/6750376#6750376
  41. 41. Security• HTTPS (encrypt channel)• Web ACL• Others (more advanced)
  42. 42. Error Handling• Use standard error message format asresponse• Use Non-200 response codesHTTP/1.1 400 Bad RequestContent-Type: application/xml;charset=UTF-8Link: http://server/error/someexplanation<error><message>You have made an invalid request...</message></error>
  43. 43. Demonstration• A simple one page web applicationhttp://mybooks.cloudfoundry.com/books.html• Restful web service is implementedusing Jersey• Client consumes the service throughJQUERY AJAX API and render the JSONdata
  44. 44. Test
  45. 45. Test RESTful Web Services• Using cURL
  46. 46. Test RESTful Web Services• Using cURL
  47. 47. Test RESTful Web Services• Using cURL
  48. 48. Test RESTful Web Services• Using SOAPUI
  49. 49. Test RESTful Web Services• Using SOAPUI
  50. 50. Test RESTful Web Services• Using SOAPUI
  51. 51. Test RESTful Web Services• Using SOAPUI
  52. 52. Test RESTful Web Services• Using SOAPUI
  53. 53. Reference• http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
  54. 54. Books

×