author: hai nguyen created date: 04/11/2008 web service introduction
TRANSCRIPT
AUTHOR: HAI NGUYENCREATED DATE: 04 /11 /2008
Web Service Introduction
Agenda
Web Service Introduction What is Web Service? Why Web Service?
XML and SchemaWeb Service: Style of UseWeb Service interactionsWeb Service elements
SOAP WSDL
Web service development
WHAT IS WEB SERVICE?
WHY WEB SERVICE?
Web Service Introduction
What is Web Service
A Software system designed to support interoperable Machine to Machine interaction over network
They are the Web APIs are hosting in machine that can be invoked by remote machine
The basic platform of Web services is Http + XML
Why web service?
Interoperability is the highest prioritySimple use
XMLSCHEMA
XML and Schema
XML
All of you already knows XML! It is very popular now and be used everywhere
We only talk some advance feature of XML
XML namespaces
Namespace is used to solve conflicts between XML element
Example 1: Conflicts of Address information<root>
<address> <name>Nextss</name>
<street>123 ABC</street> <city>HCM</city>
<country>VN</country> </address> <address> <name>Google</name> <street>123 ABC</street> <city>ABC</city> <state>ABC</state> <zipcode>xxx-xxx-xxxx</zipcode> </address></root>
XML namespaces
The namespace is defined by the xmlns attribute in the start tag of an element.
The namespace declaration has the following syntax. xmlns:prefix="URI".
XML namespaces
Example 2: Solve conflicts in data<root>
<vn:address xmlns:vn=“http://www.nextss.com/schema/vnAddress”> <vn:name>Nextss</vn:name>
<vn:street>123 ABC</vn:street> <vn:city>HCM</vn:city>
<vn:country>VN</vn:country> </vn:address> <us:address xmlns:us=“http://www.nextss.com/schema/usAddress”> <us:name>Google</us:name> <us:street>123 ABC</us:street> <us:city>ABC</us:city> <us:state>ABC</us:state> <us:zipcode>xxx-xxx-xxxx</us:zipcode> </us:address></root>
XML encoding
XML can contains non Ascii characters like Vietnamese language
The encoding errors can happen in case: XML contains non ASCII characters, and the file was
saved as single-byte ANSI (or ASCII) with no encoding specified.
Encoding specified in XML is invalid: XML file was saved as double-byte Unicode (or UTF-16) with a single-byte encoding (Windows-1252, ISO-8859-1, UTF-8) specified.
XML
SCHEMA
XML and Schema
XML Schema
XML Schema is an XML-based to describe the structure of XML document
The XML Schema language is also referred to as XML Schema Definition (XSD)
XML Schema
XML Schema defines: Elements that can appear in a document Attributes that can appear in a document Which elements are child elements The order of child elements The number of child elements Whether an element is empty or can include text Data types for elements and attributes Default and fixed values for elements and attributes
XML Schema
Example: Xml file “Note.xml”:<?xml version="1.0"?> <note>
<to>Tove</to> <from>Jani</from>
<heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
XML Schema
An XML Schema “note.xsd”:<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.nextss.com" xmlns="http://www.nextss.com" elementFormDefault="qualified"><xs:element name="note"><xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
XML Schema
Validate XML by using XML Schema<?xml version="1.0"?>
<note xmlns="http://www.nextss.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nextss.com note.xsd">
<to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>
</note>
REMOTE PROCEDURE CALLS (RPC)
RESOURCE ORIENTED
MESSAGING
Web Service: Styles of Use
Style Of Uses
Web Services RPC
RPC Web services present a distributed function (or method) call interface that is familiar to many developers. Typically, the basic unit of RPC Web services is the WSDL operation.
SOAP using a synchronous RPC programming approach and, typically, generated 'skeletons/stubs' and some sort of Object-to-XML marshalling technology
Web Service Resource Oriented
A client-server based resource-oriented pattern where the server-side provides a representation of a set of resources (often hierarchical) and exposes Create, Read, Update and Delete capabilities for these resources to client programs.
REST or 'RESTful Web Services' or ROA, re-using World-Wide-Web based approaches and standards like HTTP and URIs
Web Service Messaging
Messages or documents are passed asynchronously between peer systems in either, but not always both, directions.
SOAP using an asynchronous Message/Document passing approach where invariably the documents are defined by schemas and, often, the use of message-level (rather than transport-level) security elements is required
RPC INTERACTIONS
DOCUMENT INTERACTIONS
Web Service Interactions
RPC Interactions
RPC Interactions
The web service is viewed by the consumer as a single logical application or component with encapsulated data, where the WSDL described by the publicly-exposed interface
The messages directly map onto input and output parameters of the procedure calls or operations
RPC INTERACTIONS
DOCUMENT INTERACTIONS
Web Services Interactions
Document Interactions
Document Interactions
The service consumer interacts with the service using documents that are meant to be processed as complete entities
These exchanged documents typically take the form of XML, which is defined by a commonly agreed upon schema between the service provider and service consumer
The exchanged document represents a complete unit of information and may be completely self-describing
SOAPWSDL
Web Service Elements
SOAP
SOAP stands for Simple Object Access Protocol
SOAP is the transport methodSOAP is the format of sending messageSOAP is platform and language independentSOAP is XML-based formatSOAP is simple and extensible
SOAP
Skeleton of SOAP message<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header> ... ... </soap:Header> <soap:Body> ... ... <soap:Fault> ... ... </soap:Fault> </soap:Body> </soap:Envelope>
SOAP
WSDL
Web Service Elements
WSDL
WSDL stands for Web Services Description Language
WSDL is written in XML WSDL is an XML document WSDL is used to describe Web services WSDL is also used to locate Web services
WSDL
- Types
- Message
- PortType
- Binding
WSDL Document Structure
<types> The operations performed by the web service
<message> The messages used by the web service
<portType> The data types used by the web service
<binding> The communication protocols used by the web service
WSDL Types
The <types> element defines the data type that are used by the web service.
For maximum platform neutrality, WSDL uses XML Schema syntax to define data types.
WSDL Messages
The <message> element defines the data elements of an operation.
Each message can consist of one or more parts. The parts can be compared to the parameters of a function call in a traditional programming language.
WSDL Ports
The <message> element defines the data elements of an operation.
Each message can consist of one or more parts. The parts can be compared to the parameters of a function call in a traditional programming language.
WSDL Ports
Operation Types
One-way The operation can receive a message but will not return a response
Request-response The operation can receive a request and will return a response
Solicit-response The operation can send a request and will wait for a response
Notification The operation can send a message but will not wait for a response
SOAP Bindings
The <binding> element defines the message format and protocol details for each port.
Example:<soap:binding style="document“
transport=http://schemas.xmlsoap.org/soap/http> <operation> <soap:operation soapAction="http://example.com/getTerm”> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding>
SOAP Bindings
The soap:binding element has two attributes - the style attribute and the transport attribute.
The style attribute can be "rpc" or "document". The transport attribute defines the SOAP protocol to use.
The operation element defines each operation that the port exposes.
For each operation the corresponding SOAP action has to be defined. You must also specify how the input and output are encoded.
SOAP Bindings
A WSDL SOAP binding can be either a Remote Procedure Call (RPC) style binding or a document style binding. A SOAP binding can also have an encoded use or a literal use. There are four style/use models: RPC/encoded RPC/literal Document/encoded Document/literal
SOAP Bindings Example
We have the service interface
@WebService(name="HelloWorld“, targetNamespace="http://spring.demo/") @SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL)
public interface HelloWorld {String sayHi(@WebParam(targetNamespace="http://spring.demo/",
name="text")String text);}
SOAP Bindings Example
WSDL file for service interface:
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld"> <soap:binding style="document”
transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="sayHi"> <soap:operation soapAction="" style="document"/> <wsdl:input name="sayHi"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="sayHiResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>
SOAP Bindings Example
SOAP Request with RPC/Encoded Format <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <sayHi xmlns="http://spring.demo/"> <text xmlns="">Viet Nam</text> </sayHi> </s:Body> </s:Envelope>
SOAP Response with RPC/Encoded Format <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns1:sayHiResponse xmlns:ns1="http://spring.demo/"> <return>Hello Viet Nam</return> </ns1:sayHiResponse> </soap:Body> </soap:Envelope>
SOAP Bindings Example: RPC/Literal
WSDL with RPC/Literal Format <wsdl:message name="sayHiResponse"> <wsdl:part name="return" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHi"> <wsdl:part name="text" type="xsd:string"> </wsdl:part> </wsdl:message>
SOAP Bindings Example: RPC/Literal
SOAP Request with RPC/Literal Format <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns1:sayHi xmlns:ns1="http://spring.demo/"> <text>Viet Nam</text> </ns1:sayHi> </soap:Body> </soap:Envelope>
SOAP Response with RPC/Literal Format <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns1:sayHiResponse xmlns:ns1="http://spring.demo/"> <return>Hello Viet Nam</return> </ns1:sayHiResponse> </soap:Body> </soap:Envelope>
SOAP Bindings Example: Document/Literal
WSDL file for Document/Literal Format <wsdl:types> <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified“
targetNamespace="http://spring.demo/"> <xsd:element name="sayHi" type="tns:sayHi"/> <xsd:complexType name="sayHi"> <xsd:sequence> <xsd:element minOccurs="0" name="text" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="sayHiResponse" type="tns:sayHiResponse"/> <xsd:complexType name="sayHiResponse"> <xsd:sequence> <xsd:element minOccurs="0" name="return" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> <wsdl:message name="sayHiResponse"> <wsdl:part element="tns:sayHiResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHi"> <wsdl:part element="tns:sayHi" name="parameters"> </wsdl:part> </wsdl:message>
SOAP Bindings Example: Document/Literal
SOAP Request with Document/Literal Format <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <sayHi xmlns="http://spring.demo/" xmlns:i="http://www.w3.org/2001/XMLSchema-
instance"> <text xmlns="">Viet Nam</text> </sayHi> </s:Body> </s:Envelope>
SOAP Response with Document/Literal Format
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns1:sayHiResponse xmlns:ns1="http://spring.demo/"> <return>Hello Viet Nam</return> </ns1:sayHiResponse> </soap:Body> </soap:Envelope>
Decision on the formatting style
Validate business documents: service is accepting or returning a complex XML structure, a document style is better suited, since the XML can be validated against the schema prior to calling the service.
Performance and memory limitations: Marshalling and un-marshalling parameters to XML in memory can be an intensive process
Decision on the formatting style
Interoperability: using RPC style can causes interoperability issues across platforms. To facilitate interoperability, the WS-I Basic Profile limits the use of the encoding (RPC-encoded or document-encoded) and encourages a literal formatting (document-literal or RPC-literal style)
TWO APPROACHES OF DEVELOPING SERVICES
WEB SERVICE DEBUGGING
Web Service Development
Two approaches of developing web service
-- Code first
-- WSDL first
Code first: write the business code and web service engine will generate the WSDL by itself at compile time or runtime
WSDL first: write the WSDL first before writing interface and business code for service.
Note: Each approach has both advantages and disadvantages!
Code First
Advantage: Time of writing code is reduced. Developers mostly
focus on code only.Disadvantage:
Less interoperable among platforms/programming language! Example: - .NET client program could not invoke the web service
written in Java language - Java client program (use Axis framework) could not
invoke web service written in Java language (use Cxf framework)
WSDL First
Advantage: More interoperable among platforms/programming
language!Disadvantage:
Time of development is greater than Code first approach (includes manual update WSDL while service change)
Web Service Debugging
Log filesDebugging Tools
TCP Monitor: WSMonitor: https://wsmonitor.dev.java.net/ Tcpmon: https://tcpmon.dev.java.net/ Wireshark: http://www.wireshark.org/ Eclipse IDE
Functional Testing: SOAP UI: http://www.pushtotest.com/ PushToTest: http://www.pushtotest.com/
References
[1]. Web service - http://en.wikipedia.org/wiki/Web_service
[2]. Web Service Tutorial - http://www.w3schools.com/webservices/default.asp
[3]. SOAP Tutorial - http://www.w3schools.com/soap/default.asp
[4]. WSDL Tutorial - http://www.w3schools.com/wsdl/default.asp
[5]. Russel, Butek - Which style of WSDL should I use? http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
References
[6]. Sameer Tyagi - Patterns and Strategies for Building Document Based Web Services –http://java.sun.com/developer/technicalArticles/xml/jaxrpcpatterns/
[7]. Sameer Tyagi - Interoperability With Patterns and Strategies for Document Based Web Services - http://java.sun.com/developer/technicalArticles/xml/jaxrpcpatterns2/index.html
[8]. Paul Done - Web Services: RPC, REST and Messaging - http://dev2dev.bea.com/blog/pdone/archive/2008/02/web_services_rp.html
References
[10], Representational State Transfer - http://en.wikipedia.org/wiki/Representational_State_Transfer