webservice-WSDL結構與各元素解析

承接上一遍webservice初識,這篇文章將着重於WSDL。 wsdl協議說明http://www.w3.org/TR/wsdl


結構

現在開始說說wsdl的結構以及各個元素的意義。
從下面這張圖可以看出wsdl中各元素是存在嵌套的關係的


(reference:http://www.ibm.com/developerworks/cn/webservices/ws-wsdl/)
WSDL 文檔將Web服務定義爲服務訪問點或端口的集合。在 WSDL 中,由於服務訪問點和消息的抽象定義已從具體的服務部署或數據格式綁定中分離出來,因此可以對抽象定義進行再次使用:消息,指對交換數據的抽象描述;而端口類型,指操作的抽象集合。用於特定端口類型的具體協議和數據格式規範構成了可以再次使用的綁定。將Web訪問地址與可再次使用的綁定相關聯,可以定義一個端口,而端口的集合則定義爲服務。

繼續使用helloworld.wsdl這個例子。

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"
	name="HelloWorldService">
	<types>
		<xs:schema xmlns:tns="http://example.helloworld.com/"
			xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
			targetNamespace="http://example.helloworld.com/">
			<xs:element name="sayHello">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="arg0" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="sayHelloResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="return" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
	</types>
	<message name="sayHello">
		<part name="parameters" element="tns:sayHello"></part>
	</message>
	<message name="sayHelloResponse">
		<part name="parameters" element="tns:sayHelloResponse"></part>
	</message>
	<portType name="HelloWorld">
		<operation name="sayHello">
			<input message="tns:sayHello"></input>
			<output message="tns:sayHelloResponse"></output>
		</operation>
	</portType>
	<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document"></soap:binding>
		<operation name="sayHello">
			<soap:operation soapAction=""></soap:operation>
			<input>
				<soap:body use="literal"></soap:body>
			</input>
			<output>
				<soap:body use="literal"></soap:body>
			</output>
		</operation>
	</binding>
	<service name="HelloWorldService">
		<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
			<soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>
		</port>
	</service>
</definitions>

根元素<definitions>

<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"
	name="HelloWorldService">

......
</definitions>

文檔的根元素爲<definitions>...</definitions>,所有需要用到的namespace也在其中描述。


數據類型定義<types>

<types>可以認爲是數據容器,所有用於交換的數據類型都定義在其中,而且使用的是xml schema的語法。其更重要的用處是告訴服務端或客戶端如何將收到的xml中元素值解析成本地語言中的數據類型。

<types>
	   <xs:schema xmlns:tns="http://example.helloworld.com/"
			xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
			targetNamespace="http://example.helloworld.com/">
			<xs:element name="sayHello">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="arg0" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="sayHelloResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="return" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
	</types>

你也也可以將外數據類型定義在外部XSD文件中,然後在<types></types>中引用:

<import namespace="" location=""/>


消息格式定義<message>

<message>元素定義了一個web服務中所有交換的信息。它有一個或多個<part>的元素組成,這些M<part>不能嵌套,只能是並列關係。<types>中定義的數據類型將在這裏被用到,表示<part>的類型。

        <message name="sayHello">
		<part name="parameters" element="tns:sayHello"></part>
	</message>
	<message name="sayHelloResponse">
		<part name="parameters" element="tns:sayHelloResponse"></part>
	</message>

服務中可用操作的集合<portType>

	<portType name="HelloWorld">
		<operation name="sayHello">
			<input message="tns:sayHello"></input>
			<output message="tns:sayHelloResponse"></output>
		</operation>
	</portType>
<portType>定義了一個操作集合,它由一個或多個<operation>組成。我們可以把它理解成定義了一個函數庫或類,其中的<operation>就是具體函數或類方法。那麼其中的<input>與<output>就是方法的參數與返回值。參數和返回值得類型引用了上面<mesage>定義的消息格式。

協議綁定<binding>

<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document"></soap:binding>
		<operation name="sayHello">
			<soap:operation soapAction=""></soap:operation>
			<input>
				<soap:body use="literal"></soap:body>
			</input>
			<output>
				<soap:body use="literal"></soap:body>
			</output>
		</operation>
	</binding>
在<binding>中,之前定義的>portType>以及其中的<opeation>將會被綁定到指定的傳輸協議中,即調用表明該<portyTtpe>中某個<opeation>需使用的傳輸協議,
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" 表明使用http/soap作爲傳輸協議。


服務集合<service>

	<service name="HelloWorldService">
		<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
			<soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>
		</port>
	</service>


這部分是具體的Web服務的定義,在這個名爲HelloWorldService的Web服務中,包含了一個<port>即服務訪問入口,訪問地址是"http://localhost:8080/ws/helloWorld",使用的消息模式是由前面的binding所定義的。當然也可以定義多個使用不同協議的<port>。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章