Embed Size (px)
Transcript of RESTful services
RESTful Web Services
RESTful Web ServicesBy Pedram Bashiri
University of North Carolina at CharlotteSpring 201511Web ServerSoftware designed to serve web pages/web sites/web services. Examples are IIS, Apache, etc.
Web ServiceApplication run by a web server, performing tasks and returning structured data to a calling program, rather than html for a browser. Publicly available and standardized for use by all programmers
2Types of Web Services
33Service-Oriented Web Services Based on servicesBig Web ServicesJAX-WS = JAVA-API for XML-based Web Services, mainly using WSDL/SOAP
Resource-Oriented Web Services Based on resourcesRESTfulWeb ServicesJAX-RS = JAVA-API for RESTful Web Services, using only HTTP
44REST= REpresentational State Transfer55What REST is not !6REST is not A framework7REST is not A technology8REST is not A standard specification9REST is an architecture style10architecture styleAn architecture style is a coordinated set of architectural constraints that restricts the roles and features of architectural elements, and the allowed relationships between those elements within any architecture that conforms to that style A style can be applied to many architectures An architecture can consist of many styles11Principles of REST(Architectural Constraints)Statelessness (HTTP)Specified CacheabilityInterface Uniformity (URI)Addressability (URI)Connectedness (Hypermedia)
No Client State13StatelessnessEach request from client to server must contain all of the information necessary to understand the request and cannot take any advantage of any stored context on the server.andEach request contains all of the information necessary for a connector to understand the request, independent of any requests that may have preceded it14StatelessnessImproved visibility since a monitoring system does not have to look beyond a single requestImproved reliability due to easier recoverability from partial failuresImproved scalability due to not having to allocate resources for storing stateServer does not have to manage resource usage across requests15StatelessnessTradeoff Reduced Network PerformanceReduced server control over application consistency16Principles of REST(Architectural Constraints)Statelessness (HTTP)Specified CacheabilityInterface Uniformity (URI)Addressability (URI)Connectedness (Hypermedia)
17Specified CacheabilityData within a response to a request be implicitly or explicitly labeled as cacheable or non-cacheableIf a response is cacheable, then a client cache is given the right to reuse that response data for later, equivalent requests18Specified CacheabilityImproves efficiency, scalability and user perceived performanceTradeoff : Reduced Reliability19Principles of REST(Architectural Constraints)Statelessness (HTTP)Specified CacheabilityInterface Uniformity (URI)Addressability (URI)Connectedness (Hypermedia)
20Interface UniformityThe uniform interface simplifies and decouples the architecture, which enables each part to evolve independently.21Interface UniformityUniform Operations22In REST over HTTP these areGET= "give me some info" (Retrieve)POST= "here's some info to update (Update)PUT= "here's some new info" (Create)DELETE= "delete some info" (Delete)
23Those are the only verbs you needSimplifies semanticsSimplifies client complexitySimplifies application model24Interface UniformityTradeoff : Degrades efficiencySince Information is transferred in a standard form rather than one which is specific to application's needs25Principles of REST(Architectural Constraints)Statelessness (HTTP)Specified CacheabilityInterface Uniformity (URI)Addressability (URI)Connectedness (Hypermedia)
26Addressability through URIs27ResourcesEvery distinguishable entity is a resource. A resource may be a Web site, an HTML page, an XML document, a Web service, an image, a video etc.
28AddressabilityEvery resource is uniquely identified by a URI. The URI should generally carry no meaning to the client except as a resource locator29AddressabilityGood, clean, structured URIs are helpful for developersIf you are naming a specific single resource all the information to locate the resource should be in the URI itself and not through additional parameters30Addressabilityeg. Choosehttp://informationbase/locationdb/citiestable/punenothttp://informationbase/locator?type=city&name=pune3131Principles of REST(Architectural Constraints)Statelessness (HTTP)Specified CacheabilityInterface Uniformity (URI)Addressability (URI)Connectedness (Hypermedia)
32ConnectednessRESTful services representations are hypermedia documents. These are documents that contain not just data, but links to other resources by serving hypermedia:The quality of having links is called connectedness. Resources should link to each other in their representations. Hence, why the human web is easy to use because it is well connected.33RepresentationsResources have RepresentationsA representation captures the current or intended state of a resourceRepresentations are transferred between the client and the server34
36Why is it called "Representational State Transfer?"The Client references a Webresource using a URL. A representation of the resource is returned.The representation (e.g., Boeing747.html) places the client in a new state. When the client selects a hyperlink in Boeing747.html, it accesses another resource. The new representation places the client application into yet another state. Thus, the client application transfers state with each resource representation.
37RepresentationsA particular resource may have multiple representationsCommonly used representation formats are html, xml and jsonhowever they could also be pdf, png etc.38RepresentationsWhen multiple resource formats are supported by the server, the actual resource format returned is subject to content negotiation between the client and the server39RepresentationsThis should ideally happen through control data i.e. By using HTTP Accept headers and not by appending additional information to the URL.PreferAccept: text/xml;q=0.5, application/jsonhttp://infobase/cities/punetohttp://infobase/cities/pune.json40The Biggest RESTful system?!ReST extends the very capabilities that made WWW successful into application design and architecture
What are these characteristics of static WWW and ReST?41WWW and ReSTYou can connect to any web server if you know the home page URL
You can connect to ReST application if you know the starting URI42WWW and ReSTOn the home page you can view the content along with the appropriate hyperlinks which suggest appropriate paths for you to traverse
The response will provide you important initial content along with hyperlinks which describe their nature to navigate to other resources43WWW and ReSTYou can save the hyperlink URL, bookmark it or email it to you boss or tweet it to your friends
A ReST client can store a URI for future use or embed it as a foreign key in other resources that it maintains44WWW and ReSTYou can save the contents of any page by saving its HTML representation
You can save the representation of any resource into a XML / Document database45WWW and ReSTYou can modify the contents of the web pages by entering data in forms (and even full page content in blogs, Wikis etc.) and POSTing them.
You can perform PUT, POST and DELETE operations on resources to modify them46WWW and ReSTThe server retains no information about the pages you've traversed
The server retains no information about you or the resources you've used47WWW and ReSTDid you notice there is no global internet registry for website discovery ?
There is no registry required for ReST applications48REST and SecurityThis is one area where I choose to be non-ReSTful49REST and SecuritySometimes the deliberate requirements of security and transparency of ReST don't cooperate well50REST and SecurityCookies can help in user identification (other options being Basic HTTP authentication)Basic HTTP Authentication is weak while cookies are strictly used for user identification onlyBut cookies break the statelessness model! 51Even though it takes away their ability of beingcalled 100% ReSTful51Designing RESTful applicationsTools.NET Windows Communication Services (WCF)Java Spring MVCJAX-RS: RESTEasy, RESTLet52Designing RESTful applicationsUsing a ReST supportive framework does not make your application ReSTful53Designing RESTful applicationsYou need to model your application interfaces as a set of resourcesAnd basic CRUD operations on these resources54Designing RESTful applicationsAnswer these questions in order:What are the resources of your application? What Are the URIs? What is the representational format for the resources?What methods are supported at each URI?What status code could be returned for each method?55Designing RESTful applications
56As you move from an action oriented design towards resource oriented design, thinking of everything as nouns is one of the early challenges to overcomeTransaction.approve becomes TransactionApprovalAccount.pay becomes AccountPayment.createetc. 5757A look forward to increasing ReST popularityReST already is starting to dominate the internet space and there's a good likelihood it could dominate enterprise architectures as well.58
59References and SourcesREST Explained Representational State Transfer: presentation by Dhananjay NeneRESTful Web Services: presentation by Imran M YousufRestful web services: presentation by AngelinRESTful Web Services: presentation by Christopher Bartling, Nick Spilman, Kevin Hakansonwww.slideshare.comwww.whatisrest.comwww.Wikipedia.comwww.stackoverflow.comwww.file