introduction to restful web services
DESCRIPTION
An introduction to restful web servicesTRANSCRIPT
![Page 1: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/1.jpg)
Introduction to RESTful Web Services
09/2012
Wei Li
![Page 2: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/2.jpg)
Agenda
• RESTful in General
• URI Design
• Java Implementation
• Others
• Test
• Demonstration
![Page 3: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/3.jpg)
RESTful in General
![Page 4: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/4.jpg)
In a Nutshell
• RESTful is about resources
• RESTful is about how to represent the resource in different ways
• RESTful is about how to manipulate the resource
![Page 5: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/5.jpg)
Define REST
• REST stands for Representational State Transfer
• An architecture style for designing networked applications
![Page 6: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/6.jpg)
Define REST
• REST offers a simple, interoperable, and flexible way of writing web services that can be very different than the RPC mechanisms like CORBA and WS-*
![Page 7: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/7.jpg)
RESTful Is Not
• A protocol
• A standard
• A replacement for SOAP
![Page 8: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/8.jpg)
RESTful Architectural Principles
• Addressable resources
• A uniform, constrained interface
• Representation oriented
• Stateless communicate
• Hypermedia As The Engine Of Application State (HATEOAS)
![Page 9: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/9.jpg)
Resources
• The key abstraction of information and data in REST
• Each resource must be addressable via a URI (Uniform Resource Identifier)
• Everything can be a resource
![Page 10: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/10.jpg)
Addressability
• Every object and resource in the system should be reachable through a unique identifier
• Managed through the use of URIs
![Page 11: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/11.jpg)
Uniform and Constrained Interface
• Don’t have an “action” parameter in URI
• Use only the methods of HTTP for the web services
• HTTP has a small fixed set of operational methods. Each method has a specific purpose and meaning
![Page 12: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/12.jpg)
Representation Oriented
• The user interacts with services using representations of that service
• A resource referenced by one URI can have different formats. – HTML (for browsers)
– XML (for application)
– JSON (for JavaScript)
– Excel spreadsheet
– Image
![Page 13: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/13.jpg)
Stateless Communication
• No client session data stored on the server
• If there are needs for session-specific data, it should be held and maintained by the client and transferred to the server with each request as needed
• A service layer that does not have to maintain client sessions is much easier to scale
![Page 14: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/14.jpg)
RESTful and HTTP
• REST isn't protocol specific
• However when talking about REST, people usually mean REST over HTTP
• Benefits of using HTTP for RESTful services:
Familiarity
Interoperability
Interoperability
![Page 15: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/15.jpg)
REST Triangle
Source: http://en.wikipedia.org/wiki/File:Resttriangle.svg
![Page 16: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/16.jpg)
HTTP Methods
• GET
• PUT
• DELETE
• POST
• HEAD
• OPTIONS
![Page 17: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/17.jpg)
CRUD Operation Mapped to HTTP Methods in RESTful
OPERATION HTTP METHOD
Create POST
Read GET
Update PUT
Delete DELETE
![Page 18: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/18.jpg)
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
![Page 19: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/19.jpg)
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
![Page 20: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/20.jpg)
Create a Resource
Source: Rest in Practice
![Page 21: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/21.jpg)
Read a Resource
Source: Rest in Practice
![Page 22: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/22.jpg)
Update a Resource
Source: Rest in Practice
![Page 23: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/23.jpg)
Delete a Resource
Source: Rest in Practice
![Page 24: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/24.jpg)
URI Design
![Page 25: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/25.jpg)
URI
• Human meaningful
• Hierarchical
• Nouns
• No verbs
![Page 26: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/26.jpg)
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
![Page 27: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/27.jpg)
URI Examples
• Twitter REST API v1.1 example: – https://dev.twitter.com/docs/api/1.1
![Page 28: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/28.jpg)
Content Negotiation
![Page 29: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/29.jpg)
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
![Page 30: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/30.jpg)
Content Negotiation and URI
• https://cwiki.apache.org/WINK/jax-rs-content-negotiation.html
![Page 31: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/31.jpg)
Java Implementation
![Page 32: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/32.jpg)
JSR-311
• POJO based
• HTTP centric
• Format independence
• Container independence
• Inclusion in Java EE
![Page 33: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/33.jpg)
JSR-311 Annotations
![Page 34: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/34.jpg)
JSR 311 Annotations
![Page 35: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/35.jpg)
Available Java Frameworks
• Jersey
• CXF
• Wink
• RESTEasy
![Page 36: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/36.jpg)
Others
![Page 37: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/37.jpg)
WADL
• The Web Application Description Language
• A machine-readable XML description of HTTP-based web applications (typically RESTful web services).
![Page 38: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/38.jpg)
WADL
• Example (using Jersey implementation)
http://localhost:9999/restapi/application.wadl
![Page 39: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/39.jpg)
Versioning
• Put the version in URI
http://localhost:9999/restapi/v2.0/books
![Page 40: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/40.jpg)
Versioning
• put the version in the Accept Header of the request
http://stackoverflow.com/questions/389169/best-practices-for-api-versioning/6750376#6750376
![Page 41: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/41.jpg)
Security
• HTTPS (encrypt channel)
• Web ACL
• Others (more advanced)
![Page 42: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/42.jpg)
Error Handling
• Use standard error message format as response
• Use Non-200 response codes
HTTP/1.1 400 Bad Request
Content-Type: application/xml;charset=UTF-8
Link: http://server/error/someexplanation
<error>
<message>You have made an invalid request...
</message>
</error>
![Page 43: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/43.jpg)
Demonstration
• A simple one page web application http://mybooks.cloudfoundry.com/books.html
• Restful web service is implemented using Jersey
• Client consumes the service through JQUERY AJAX API and render the JSON data
![Page 44: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/44.jpg)
Test
![Page 45: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/45.jpg)
Test RESTful Web Services
• Using cURL
![Page 46: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/46.jpg)
Test RESTful Web Services
• Using cURL
![Page 47: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/47.jpg)
Test RESTful Web Services
• Using cURL
![Page 48: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/48.jpg)
Test RESTful Web Services
• Using SOAPUI
![Page 49: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/49.jpg)
Test RESTful Web Services
• Using SOAPUI
![Page 50: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/50.jpg)
Test RESTful Web Services
• Using SOAPUI
![Page 51: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/51.jpg)
Test RESTful Web Services
• Using SOAPUI
![Page 52: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/52.jpg)
Test RESTful Web Services
• Using SOAPUI
![Page 53: Introduction to Restful Web Services](https://reader034.vdocuments.mx/reader034/viewer/2022050815/548b4838b479590f0d8b606f/html5/thumbnails/53.jpg)
Reference
• http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm