Introduction to Web Services By J. H. Wang Nov. 28, 2011 Outline • Overview • RESTful Web services What is a Web Service • “a method of communication between two electronic devices over the Web” – From Wikipedia entry on “Web service” • “a software system designed to support interoperable machine-to-machine interaction over a network” – From W3C definition • Web service – An interface described in a machine-processable format (WSDL, or Web Services Description Language) – Other systems interact with the Web service using SOAP messages, typically conveyed using XML/HTTP and other Web-related standards • SOAP: Simple Object Access Protocol • Two major classes of Web services – REST-compliant • To manipulate XML representations of Web resources using a uniform set of “stateless” operations – Arbitrary Web Services Architecture Simple Object Access Protocol (SOAP) • A protocol for exchanging structured information in the implementation of Web Services – XML: for message format – HTTP, SMTP: for message transmission SOAP Message • POST /InStock HTTP/1.1 Host: www.example.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: 299 SOAPAction: "http://www.w3.org/2003/05/soapenvelope" <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05 /soap-envelope"> <soap:Header> </soap:Header> <soap:Body> <m:GetStockPrice xmlns:m="http://www.example.org/stock" > <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope> Web APIs • Moving from SOAP based services to REST based communications – REST: Representational State Transfer • Do not require XML, SOAP, WSDL • Typically a defined set of HTTP request messages along with the structure of response messages expressed in XML or JSON format – JSON: JavaScript Object Notation • They allow the combination of multiple Web services into new applications known as mashups Web Services in a Service-Oriented Architecture Three Most Common Styles of Use • RPC (Remote Procedure Calls) – A distributed function call interface • SOA (Service-Oriented Architecture) – The basic unit of communication is a message, rather than an operation • REST (Representational State Transfer) – Standard operations in HTTP: GET, POST, PUT, DELETE – Interacting with stateful resources, rather than messgaes or operations RPC Web Services • Basic unit: WSDL operation • Widely deployed and supported, but not loosely coupled • Other approaches: CORBA, DCE/RPC, Java RMI SOA Web Services • Basic unit: message • Supported by most major vendors, loose coupling Representational State Transfer (REST) • Interacting with stateful resources, rather than messages or operations • Using HTTP standard operations such as GET, POST, PUT, DELETE • WSDL 2.0 offers support for binding to all HTTP request methods – WSDL 1.1 only GET and POST Representation of concepts in WSDL 1.1 and 2.0 documents Criticisms • Too complex, not open source • A custom interface requires a custom client for every service • Concerns about performance due to XML and SOAP/HTTP in enveloping and transport RESTful Web Services • Introduced by Roy Fielding in his doctoral dissertation – He is one of the principal authors of the HTTP specification version 1.0 and 1.1 • Client-server – Clients initiate requests – Servers process requests and return appropriate responses – Requests and responses are built around the transfer of representations of resources Constraints • Client-server • Stateless – No client context is stored on the server between requests – The server can be stateful • Cacheable – Clients can cache responses • Layered system – Clients cannot tell whether it’s connected directly to the end server, or an intermediary • Code on demand (optional) – Servers are able to temporarily extend the functionality of a client • Uniform interface Guiding Principles of the Interface • Identification of resources – E.g. URIs • Manipulation of resources through these representations • Self-descriptive messages • Hypermedia as the engine of application state – E.g. hyperlinks, hypertext Key Goals • • • • Scalability of component interactions Generality of interfaces Independent deployment of components Intermediary components to reduce latency, enforce security, and encapsulate legacy systems RESTful Web API • Four aspects – Base URI for the Web service – Internet media type of the data supported by the Web service • E.g. JSON, XML, or YAML – The set of operations supported by the Web service using HTTP methods • E.g. GET, PUT, POST, or DELETE – The API must be hypertext driven • No official standard for RESTful services – But Web standard protocols are often used RESTful Web services: Basics • • • • Use HTTP methods explicitly Be stateless Expose directory structure-like URIs Transfer XML, JSON, or both Using HTTP Methods Explicitly • One-to-one mapping – – – – GET: to retrieve a resource on the server POST: to create a resource PUT: to change the state of a resource or to update it DELETE: to remove a resource • For example, – Before • GET /adduser?name=Robert HTTP/1.1 – After • POST /users HTTP/1.1 Host: myserver Content-Type: application/xml <?xml version="1.0"?> <user> <name>Robert</name> </user> • Another example – Before • GET /updateuser?name=Robert&newname=Bob HTTP/1.1 – After • PUT /users/Robert HTTP/1.1 Host: myserver Content-Type: application/xml <?xml version="1.0"?> <user> <name>Bob</name> </user> Be Stateless • For scalability, clients are required to send complete, independent requests – include all data needed to be fulfilled so that the components in the intermediary servers may forward, route, and load-balance without any state being held locally in between requests Expose directory structure-like URIs • Ex. – http://www.myservice.org/discussion/topics/{topic} – http://www.myservice.org/discussion/2008/12/10/{topic} • Guidelines – Hide the server-side scripting technology file extensions (.jsp, .php, .asp), if any, so you can port to something else without changing the URIs – Keep everything lowercase – Substitute spaces with hyphens or underscores (one or the other) – Avoid query strings as much as you can – Instead of using the 404 Not Found code if the request URI is for a partial path, always provide a default page or resource as a response. Transfer XML, JSON, or both • Ex. – <?xml version="1.0"?> <discussion date="{date}" topic="{topic}"> <comment>{comment}</comment> <replies> <reply from="joe@mail.com" href="/discussion/topics/{topic}/joe"/> <reply from="bob@mail.com" href="/discussion/topics/{topic}/bob"/> </replies> </discussion> • Common MIME types – JSON: application/json – XML: application/xml – XHTML: application/xhtml+xml References • http://en.wikipedia.org/wiki/Web_service • RESTful Web services: the basics, by Alex Rodriguez, IBM developerWorks, available at: http://www.ibm.com/developerworks/webs ervices/library/ws-restful/. Thanks for Your Attention!