1 / 51

RESTful Web Services

RESTful Web Services. ( An Introduction ). REST. An Architectural style not an implementation (http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm) Each resource has a URI (Universal Resource Identifier) Exchange resource representations (XML, JSON,RDF)

maeve
Télécharger la présentation

RESTful Web Services

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RESTful Web Services ( An Introduction )

  2. REST • An Architectural style not an implementation(http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm) • Each resource has a URI (Universal Resource Identifier) • Exchange resource representations • (XML, JSON,RDF) • Uniform interface semantics (CRUD API): HTTP protocolCRUD actionDescription POST CREATE Create a new resource GET RETRIEVE Retrieve a resource representation PUT UPDATE Update a resource DELETE DELETE Deletea resource a resource

  3. REST = Representational State Transfer • Defined by Roy Fielding (2000 Ph.D) (http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm) • A style of software architecture for distributed hypermedia systems. • Based on HTTP • Four Interface Constraints • Identification of Resources • Manipulation of Resources through Representations • Self descriptive messages • Hypermedia as the engine of application state

  4. RESTful • A system that meets the qualities of the REST architectural style is termed RESTful. • A lot of subtleties in the use of HTTP and URI naming schemes. • Stateless interactions - self-descriptive messages • Everything in a RESTful system is a resource – a noun • Easily supported by any programming language with HTTP support • Utilises HTTP Headers and Response Codes • RESTful Web Services (e.g. Flickr) can be invoked directly from browser via JavaScript

  5. Representational State Transfer • Each Resource has its own URI • A Resource is any information that can be named: documents, images, services, people, collections, etc. • The client • References a Resource via its URI • A representation of the resource is returned • The representation places the client in a state • The client references a second Resource via its URI • The new representation places the client in a new state • The client application changes (transfers) state with each resource representation

  6. Firefox plus Tabulator Extension http://www.3kbo.com/people/irene.bell-hancock/foaf.rdf#me irene_at_thira.jpg Person foaf:depiction Photo foaf:knows http://www.3kbo.com/people/richard.hancock/foaf.rdf#i richard-hancock.jpg Person foaf:depiction Photo owl:sameAs http://www.abeservices.com.au/people/rhancock/foaf.rdf#rhancock richard-hancock.jpg Person foaf:depiction Photo

  7. Irene’s FOAF.RDF <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:foaf="http://xmlns.com/foaf/0.1/"> <foaf:PersonalProfileDocument rdf:about=""> <foaf:maker rdf:resource="#me"/> <foaf:primaryTopic rdf:resource="#me"/> </foaf:PersonalProfileDocument> <foaf:Person rdf:ID="me"> <foaf:name>Irene Bell-Hancock</foaf:name> <foaf:title>Ms</foaf:title> <foaf:givenname>Irene</foaf:givenname> <foaf:family_name>Bell-Hancock</foaf:family_name> <foaf:mbox_sha1sum>e39942eeef2b9fa59fc908180c27d6e81588b21f</foaf:mbox_sha1sum> <foaf:homepage rdf:resource="http://picasaweb.google.com/goannagraphics"/> <foaf:depiction rdf:resource="irene_at_thira.jpg"/> <foaf:workplaceHomepage rdf:resource="http://www.tlc.co.nz/"/> <foaf:knows> <foaf:Person rdf:about="http://www.3kbo.com/people/richard.hancock/foaf.rdf#i"> <foaf:name>Richard Hancock</foaf:name> <foaf:mbox_sha1sum>e654ea652c61d67ecda13df3a990bff6e62ebbff</foaf:mbox_sha1sum> <rdfs:seeAlso rdf:resource="http://www.3kbo.com/people/richard.hancock/foaf.rdf"/> </foaf:Person> </foaf:knows> </foaf:Person> </rdf:RDF>

  8. Richards FOAF.RDF (1) <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:foaf="http://xmlns.com/foaf/0.1/"> <foaf:PersonalProfileDocument rdf:about=""> <foaf:maker rdf:resource="#i"/> <foaf:primaryTopic rdf:resource="#i"/> </foaf:PersonalProfileDocument> <foaf:Person rdf:ID="i"> <foaf:name>Richard Hancock</foaf:name> <foaf:givenname>Richard</foaf:givenname> <foaf:family_name>Hancock</foaf:family_name> <owl:sameAs rdf:resource="http://www.abeservices.com.au/people/rhancock/foaf.rdf#rhancock"/> <foaf:mbox_sha1sum>2a2462a095d24603cee3485d5132092c1d5e70cc</foaf:mbox_sha1sum> <foaf:homepage rdf:resource="http://www.3kbo.com/people/"/> <foaf:depiction rdf:resource="richard-hancock.jpg"/> <foaf:workplaceHomepage rdf:resource="http://www.abeservices.com.au/"/> <foaf:workInfoHomepage rdf:resource="Software Architect and Developer"/> <foaf:holdsAccount> <foaf:OnlineAccount> <foaf:accountServiceHomepage rdf:resource="http://del.icio.us"/> <foaf:accountName>breakerbay</foaf:accountName> </foaf:OnlineAccount> </foaf:holdsAccount> <foaf:knows> <foaf:Person rdf:about="http://www.3kbo.com/people/irene.bell-hancock/foaf.rdf#me"> <foaf:name>Irene</foaf:name> <foaf:mbox_sha1sum>e39942eeef2b9fa59fc908180c27d6e81588b21f</foaf:mbox_sha1sum> <rdfs:seeAlso rdf:resource="http://www.3kbo.com/people/irene.bell-hancock/foaf.rdf"/> </foaf:Person> </foaf:knows> … </foaf:Person> </rdf:RDF>

  9. Richards FOAF.RDF (2) <?xml version="1.0"?> <rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:cc="http://web.resource.org/cc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <foaf:PersonalProfileDocument rdf:about="http://www.abeservices.com.au/people/rhancock/foaf.rdf"> <dc:description>FOAF Profile of Richard Hancock</dc:description> <dc:creator>Richard Hancock</dc:creator> <foaf:primaryTopic> <foaf:Person rdf:about="http://www.abeservices.com.au/people/rhancock/foaf.rdf#rhancock"> <rdfs:seeAlso rdf:resource="http://www.abeservices.com.au/people/rhancock/foaf.rdf"/> <foaf:name>Richard Hancock</foaf:name> <foaf:firstname>Richard</foaf:firstname> <foaf:family_name>Hancock</foaf:family_name> <foaf:nick>Richard</foaf:nick> <foaf:nick>breakerbay</foaf:nick> <foaf:mbox_sha1sum>39f3c9b7479a83c76596a7c92b61f76dee3f5343</foaf:mbox_sha1sum> <foaf:depiction rdf:resource="http://www.abeservices.com.au/people/rhancock/richard-hancock.jpg"/> … </foaf:PersonalProfileDocument> </rdf:RDF>

  10. Irene’s FOAF Page (1) http://www.3kbo.com/people/irene.bell-hancock/foaf.rdf#me

  11. Irenes FOAF Page (2)

  12. Irenes FOAF Page (3)

  13. Representations • A resource may have multiple representations • XHTML, XML, JSON, binary (e.g., jpeg), name/value pairs, RDF • English, Japanese, etc. • Representations should be well-known media types (IANA registered MIME types) • Client can ask for a particular representations via request headers • Accept: text/xml,application/xml,application/xhtml+xml, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 • Accept-Language: en-us,en;q=0.5 • Accept-Encoding: gzip,deflate • Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 • “q” = relative preference (0-1)

  14. XHTML Mobile Profile

  15. JSON new Ajax.Request('/cities', { method:'get', requestHeaders: {Accept: 'application/json'}, onSuccess: function(transport){ var cities = transport.responseText.evalJSON(true); } }); var cities = [ { "name" : "Auckland", "lat" : -36.86619984752065, "lng" : 174.76692019736444, "count" : 77 }, ... { "name" : "Wellington", "lat" : -41.28276042001503, "lng" : 174.77935722880733, "count" : 123 } ];

  16. Directory of New Zealand Libraries

  17. RESTful Design Process • Decide on the resources (nouns) and their descriptive URIs. • Choose URIs that while not change over time and are implementation independent • Choose a data format for communication on each URI (xhtml, xml, json, rdf). • Specify the methods on each resource. • Specify the returned data and status codes.

  18. Implementation Independent URIs • RESTful: • http://projects.3kbo.com/portfolios/1/sites • Less RESTful: • http://abeserver.isa.net.au:9090/wx/compliance/jobs.jsp?projectId=55

  19. Designing Implementation Independent URIs • http://abeserver.isa.net.au:9090/wx/compliance/jobs.jsp?projectId=55 • http://abeserver.isa.net.au:9090/wx/compliance/projects/55/jobs • http://cdms.mobi/compliance/projects/55/jobs • Additional aspects: • Shorter URI’s are quicker to key on a mobile phone browser. • Some browsers don’t cache URL’s that contain query parameters. • Easy to bookmark

  20. Napier Public Library http://directory.natlib.govt.nz/library-symbols-web/library/NAP

  21. Resources expose a uniform interface via HTTP HTTP protocolCRUD actionDescription POST CREATE Create a new resource GET RETRIEVE Retrieve a resource representation PUT UPDATE Update a resource DELETE DELETE Deletea resource resource

  22. CRUD Actions • Change Application (Resource) State on Server • Provide Representation to Client

  23. Create, Update, Delete Resources • PUT: Not safe, idempotent • Create a new resource at the specified URI • Update existing resource at the specified URI • Typical response: 201 Created or 400 Bad Request • DELETE: Not safe, idempotent • Remove the resource at the specified URI • Typical response: 200 OK • POST: Not safe, not idempotent • Create a subordinate resource • Create child of resource at specified URI • Create new resource from factory resource at specified URI • Typical response: 201 Created + Location or 400 Bad Request • Append representation to existing resource

  24. The Writable Web

  25. GET - Collections as XHTML

  26. GET - Collections as XML

  27. POST - Create Resource

  28. HTTP GET - Resource as XML

  29. Demo

  30. The Writable Web • Some HTTP clients, servers, or middleware don’t understand PUT & DELETE • Including HTML forms and some firewalls/proxy servers • Valid to overload POST to mimic PUT and DELETE in this case: • Example: POST /123/line+items/2?_method=delete

  31. HTTP Response Codes • HTTP has more response codes than 200 and 404 • For Example: • Information: 1xx, Success 2xx, Redirection 3xx, Client Error 4xx, Server Error 5xx • For GETs: • 200 OK • 204 No Content • 301 Not Modified • 303 See Other - Used for Content Negotiation • 404 Not Found • For POST Create: • 201

  32. Conditional HTTP Get • Give me a resource representation, but not if it hasn’t changed since last time I asked. • Saves client and server time and bandwidth • Implemented with two response headers • Last-Modified • ETag • And two request headers • If-Modified-Since • If-None-Match

  33. Conditional GET: Last-Modified Client Server GET /expenses;pending 200 OK Last-Modified: Sun, 21 Jan 2007 09:35:19 GMT [response] GET /expenses;pending If-Modified-Since: Sun, 21 Jan 2007 09:35:19 GMT 304 Not Modified

  34. Conditional HTTP Get • Last-Modified sent by server, indicates when the underlying representation was last changed • Client stores the time value of Last-Modified to use later • The next time the client requests the resource it can provide the time in the If-Modified-Since header • If data changed between request send new representation in the entity body • 200 (“OK”) • If data not changed omit the entity body • 301 (“Not Modified”) • Client reuses cached representation • Saves time and bandwidth

  35. Conditional GET: ETag Mechanism Client Server GET /expenses;pending 200 OK ETag:”some UID” [response] GET /expenses;pending If-None-Match:”some UID” 304 Not Modified • On the first request the client receives the resource and receives and stores an ETag. • On subsequent requests the client sends along that ETag in an If-None-Match: header. • This turns a regular GET into a "conditional" GET. • When the server receives that conditional GET, if the resource hasn't changed then the response returned is a 304 Not Modified with no response body. • If the resource has changed then a 200 Ok would be returned.

  36. ETag • Used to indicate a change in representation • The Apache server uses filesystem information like file size and modification time to generate Etag headers for static files without reading their contents. • The Etag header doesn’t need to incorporate every bit of data in the representation: it just has to change whenever the representation changes.

  37. If-None-Match • Last-Modified is not totally reliable, one second resolution too coarse grained. • The If-None-Match request header is to Etag as the If-Modified-Since request header is to Last-Modified. • The condition is “the representation has changed, as embodied in the entity tag.”

  38. Server • If a server provides both Last-Modified and Etag, the client can provide both If-Modified-Since and If-None-Match in subsequent requests • The server should make both checks • It should only send a new representation if the representation has changed and the Etag is different.

  39. Cache Control /** * No built-in cache control in Tomcat. * This filter allows a cache-control header to be added for the resources such as images, * by adding the following to the web.xml file. * <filter> * <filter-name>ResponseHeaderFilter</filter-name> * <filter-class>nz.govt.natlib.symbols.web.filters.ResponseHeaderFilter</filter-class> * <init-param> * <param-name>Cache-Control</param-name> * <param-value>max-age=3600</param-value> * </init-param> * </filter> * public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; // set the provided HTTP response parameters for (Enumeration e=fc.getInitParameterNames();e.hasMoreElements();) { String headerName = (String)e.nextElement(); response.addHeader(headerName, fc.getInitParameter(headerName)); } // pass the request/response on chain.doFilter(req, response); }

  40. Frameworks with REST Support • RESTlet (Java, open source) • JSR 311/JAX-RS (Java) • Struts 2 REST Plugin (Java, open source) • SpringMVC + RESTlet routers + JSR311 annotations • Ruby on Rails 2.0 (Ruby, open source) • Grails 1.0 (GroovyandJava) • Project Zero (Groovy andPHP) • Django (Python, open source) • .NET 3.5 (Microsoft)

  41. JAX-RS (JSR 311) • A Java API for supporting Web Services via annotations • https://jsr311.dev.java.net/ • Jersey is the open source JAX-RS (JSR 311) Reference Implementation • https://jersey.dev.java.net/ • Sun Web Developer Pack (SWDP) includes an early access implementation of a Java API for RESTful web services development. • http://developers.sun.com/web/swdp/

  42. RESTful Web Services • Yahoo • Flickr • Amazon • Google - E.g. OpenSocial API • Etc.. • http://programmableweb.com

  43. REST Summary • An architectural style • Resources are addressable (URIs) • Resources are first class objects • Interact with representations of resources • State is maintained within a resource representation • Small set of HTTP Methods applied to any resource • Resource oriented rather than action oriented. • Scaleable

  44. References RESTful Web Services by Leonard Richardson and Sam Ruby O’Reilly Architectural Styles and the Design of Network-based Software Architectures by Roy Fielding - principal author of the HTTP 1.0, HTTP 1.1, and URI specs How to Publish Linked Data on the Web Burton Group RESTEasy Presentation

  45. Additional Material

  46. Content Negotiation • An RDF client sends an HTTP GET request to a URI, including an Accept: application/rdf+xml header with the request. ( A HTML browser would send an Accept: text/html header instead.) • 2. The server recognizes the URI but can not return the requested representation so it answers using the HTTP 303 See Other response code and sends the client the URI of the required resource, the RDF content location. • 3. The client now asks the server to GET a representation using the provided URI, again requesting application/rdf+xml. • 4. The server sends the client a RDF/XML document

  47. Content Negotiation Overview The picture shows how dereferencing a HTTP URI identifying a non-information resource plays together with content negotiation

  48. Summary of REST constraints

  49. HTTP • A stateless, client/server, request/response application protocol • Clients make requests of servers using four main operations (uniform interface) • GET, POST, PUT, DELETE (there are others) • A request is sent to a specified URI (addressabilty) • An HTTP request/response is an envelope. • Inside the envelope are headers followed by a resource representation—if any (self-descriptive)

  50. Burton Group Awareness of REST as a simpler and more scalable means of delivering network accessible services continues to rise, as does frustration with the complexity and interoperability issues of the web services framework (WSF). Representational State Transfer (REST) is an architectural style for distributed systems that provides constraints on component interaction in order to maximize the scalability and evolvability of networked applications. The most ubiquitous implementation of the REST style is the Web itself. • Peter Lacey (Senior Consultant, Member of the JCP process for JSR 311 (Java API for RESTful Web Services).) • Runs a Workshop which provides a: • Explanation of REST • How it compares to SOAP and the WSF. • The architectural underpinnings of REST: identifiable resources, a uniform interface, resource representations, statelessness, and hypermedia. • A working, RESTful system will be demonstrated using the Java Restlet Framework. • How to design and build RESTful applications • Guidelines for evaluating REST vs. SOAP for enterprise systems. http://www.burtongroup.com/Guest/Aps/RestWorkshop.aspx

More Related