WSDL實(shí)例解析
關(guān)鍵字: web serviceWSDL的主要文檔元素
WSDL文檔可以分為兩部分。頂部分由抽象定義組成,而底部分則由具體描述組成。抽象部分以獨(dú)立于平臺(tái)和語言的方式定義SOAP消息,它們并不包含任何隨 機(jī)器或語言而變的元素。這就定義了一系列服務(wù),截然不同的應(yīng)用都可以實(shí)現(xiàn)。具體部分,如數(shù)據(jù)的序列化則歸入底部分,因?yàn)樗唧w的定義。在上述的文檔元 素中,<types>、<message>、<portType>屬于抽象定義 層,<binding>、<service>屬于具體定義層。所有的抽象可以是單獨(dú)存在于別的文件中,也可以從主文檔中導(dǎo)入。
WSDL文檔的結(jié)構(gòu)實(shí)例解析
下面我們將通過一個(gè)實(shí)際的WSDL文檔例子來詳細(xì)說明各標(biāo)簽的作用及關(guān)系。
- <?xml version="1.0" encoding="UTF-8"?>
- <definitions
- xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
- xmlns:tns="http://www.jsoso.com/wstest"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns="http://schemas.xmlsoap.org/wsdl/"
- targetNamespace="http://www.jsoso.com/wstest"
- name="Example">
- <types>
- <xsd:schema>
- <xsd:import
- namespace="http://www.jsoso.com/wstest"
- schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
- </xsd:schema>
- </types>
- <message name="toSayHello">
- <part name="userName" type="xsd:string"></part>
- </message>
- <message name="toSayHelloResponse">
- <part name="returnWord" type="xsd:string"></part>
- </message>
- <message name="sayHello">
- <part name="person" type="tns:person"></part>
- <part name="arg1" type="xsd:string"></part>
- </message>
- <message name="sayHelloResponse">
- <part name="personList" type="tns:personArray"></part>
- </message>
- <message name="HelloException">
- <part name="fault" element="tns:HelloException"></part>
- </message>
- <portType name="Example">
- <operation name="toSayHello" parameterOrder="userName">
- <input message="tns:toSayHello"></input>
- <output message="tns:toSayHelloResponse"></output>
- </operation>
- <operation name="sayHello" parameterOrder="person arg1">
- <input message="tns:sayHello"></input>
- <output message="tns:sayHelloResponse"></output>
- <fault message="tns:HelloException" name="HelloException"></fault>
- </operation>
- </portType>
- <binding name="ExamplePortBinding" type="tns:Example">
- <soap:binding
- transport="http://schemas.xmlsoap.org/soap/http"
- style="rpc"></soap:binding>
- <operation name="toSayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- </operation>
- <operation name="sayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- <fault name="HelloException">
- <soap:fault name="HelloException" use="literal"></soap:fault>
- </fault>
- </operation>
- </binding>
- <service name="Example">
- <port name="ExamplePort" binding="tns:ExamplePortBinding">
- <soap:address location="http://localhost:8080/hello"></soap:address>
- </port>
- </service>
- </definitions>
由于上面的事例XML較長,我們將其逐段分解講解
WSDL文檔的根元素:<definitions>
- <definitions
- xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
- xmlns:tns="http://www.jsoso.com/wstest"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns="http://schemas.xmlsoap.org/wsdl/"
- targetNamespace="http://www.jsoso.com/wstest"
- name="Example">
- ……
- ……
- </definitions>
<definitions>定義了文檔中用到的各個(gè)xml元素的namespace縮寫,也界定了本文檔自己的 targetNamespace="http://www.jsoso.com/wstest",這意味著其它的XML要引用當(dāng)前XML中的元素時(shí),要聲 明這個(gè)namespace。注意xmlns:tns="http://www.jsoso.com/wstest"這個(gè)聲明,它標(biāo)示了使用tns這個(gè)前綴 指向自身的命名空間。
WSDL文檔數(shù)據(jù)類型定義元素:<types>
- <types>
- <xsd:schema>
- <xsd:import
- namespace="http://www.jsoso.com/wstest"
- schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
- </xsd:schema>
- </types>
<types>標(biāo)簽定義了當(dāng)前的WSDL文檔用到的數(shù)據(jù)類型。要說明的是,為了最大程度的平臺(tái)中立性,WSDL 使用 XML Schema 語法來定義數(shù)據(jù)類型。這些數(shù)據(jù)類型用來定義web service方法的參數(shù)和返回指。對(duì)于通用的原生數(shù)據(jù)類型如:integer , boolean , char , float等,在W3C的標(biāo)準(zhǔn)文檔http://www.w3.org/2001/XMLSchema中已經(jīng)做了定義。這里我們要引入的schema定義 schemaLocation="http://localhost:8080/hello?xsd=1"是我們自定義的Java對(duì)象類型。
WSDL文檔消息體定義元素:< message >
- <message name="toSayHello">
- <part name="userName" type="xsd:string"></part>
- </message>
- <message name="toSayHelloResponse">
- <part name="returnWord" type="xsd:string"></part>
- </message>
- <message name="sayHello">
- <part name="person" type="tns:person"></part>
- <part name="arg1" type="xsd:string"></part>
- </message>
- <message name="sayHelloResponse">
- <part name="personList" type="tns:personArray"></part>
- </message>
- <message name="HelloException">
- <part name="fault" element="tns:HelloException"></part>
- </message>
<message>元素定義了web service函數(shù)的參數(shù)。<message>元素中的每個(gè)<part>子元素都和某個(gè)參數(shù)相符。輸入?yún)?shù)在<message>元素中定義,與輸出參數(shù)相 隔離,輸出參數(shù)有自己的<message>元素。兼作輸入、輸出的參數(shù)在輸入輸出的<message>元素中有它們相應(yīng)的<part>元素。輸出 <message>元素以"Response"結(jié)尾,對(duì)Java而言方法得返回值就對(duì)應(yīng)一個(gè)輸出的<message>。每個(gè)<part>元素都有名字和類 型屬性,就像函數(shù)的參數(shù)有參數(shù)名和參數(shù)類型。
在上面的文檔中有兩個(gè)輸入?yún)?shù)、兩個(gè)輸出參數(shù)和一個(gè)錯(cuò)誤參數(shù)(對(duì)應(yīng)Java中的Exception)。
? 輸入?yún)?shù)<message>的name屬性分別命名為toSayHello,sayHello。
toSayHello對(duì)應(yīng)輸入?yún)?shù)userName,參數(shù)類型為xsd:string,在Java語言中就是String;
sayHello對(duì)應(yīng)兩個(gè)輸入?yún)?shù)person和arg1,類型為tns:person和xsd:string。這里tns:person類型就是引用了< types >標(biāo)簽中的類型定義。
? 輸出參數(shù)<message>的name屬性分別命名為toSayHelloResponse和sayHelloResponse。
這個(gè)名稱和輸入?yún)?shù)的<message>標(biāo)簽name屬性對(duì)應(yīng),在其后面加上Response尾綴。
toSayHelloResponse對(duì)應(yīng)的返回值是returnWord,參數(shù)類型為xsd:string;
sayHelloResponse對(duì)應(yīng)的返回值是personList,參數(shù)類型為tns:personArray(自定義類型);
? 錯(cuò)誤參數(shù)<message>的name屬性為HelloException。
它的<part>子標(biāo)簽element而不是type來定義類型。
以上的message標(biāo)簽的name屬性通常使用web service函數(shù)方法名作為參照,錯(cuò)誤參數(shù)標(biāo)簽則使用異常類名為參照。標(biāo)簽中的參數(shù)名稱,即part子元素的name屬性是可自定義的(下一章節(jié)詳細(xì)說 明)。message標(biāo)簽的參數(shù)類型將引用types標(biāo)簽的定義。
WSDL文檔函數(shù)體定義元素:< portType >
- <portType name="Example">
- <operation name="toSayHello" parameterOrder="userName">
- <input message="tns:toSayHello"></input>
- <output message="tns:toSayHelloResponse"></output>
- </operation>
- <operation name="sayHello" parameterOrder="person arg1">
- <input message="tns:sayHello"></input>
- <output message="tns:sayHelloResponse"></output>
- <fault message="tns:HelloException" name="HelloException"></fault>
- </operation>
- </portType>
在<operation>元素中,name屬性表示服務(wù)方法名,parameterOrder屬性表示方法的參數(shù)順序,使用空格符分割多個(gè)參 數(shù),如:“parameterOrder="person arg1”。<operation>元素的子標(biāo)簽<input>表示輸入?yún)?shù)說明,它引用<message>標(biāo)簽中的輸入?yún)?數(shù)。<output>表示輸出參數(shù)說明,它引用<message>標(biāo)簽中的輸出參數(shù)。<fault>標(biāo)簽在Java方法中的特別 用來表示異常(其它語言有對(duì)應(yīng)的錯(cuò)誤處理機(jī)制),它引用<message>標(biāo)簽中的錯(cuò)誤參數(shù)。
WSDL綁定實(shí)現(xiàn)定義元素:< binding >
- <binding name="ExamplePortBinding" type="tns:Example">
- <soap:binding
- transport="http://schemas.xmlsoap.org/soap/http"
- style="rpc"></soap:binding>
- <operation name="toSayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- </operation>
- <operation name="sayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- <fault name="HelloException">
- <soap:fault name="HelloException" use="literal"></soap:fault>
- </fault>
- </operation>
- </binding>
<binding>標(biāo)簽是完整描述協(xié)議、序列化和編碼的地方,<types>,<message>和<portType>標(biāo)簽處理抽象的數(shù)據(jù)內(nèi)容,而<binding>標(biāo)簽是處理數(shù)據(jù)傳輸?shù)奈锢韺?shí)現(xiàn)。
<binding>標(biāo)簽把前三部分的抽象定義具體化。
首先<binding>標(biāo)簽使用<soap:binding>的transport和style屬性定義了Web Service的通訊協(xié)議HTTP和SOAP的請(qǐng)求風(fēng)格RPC。其次<operation>子標(biāo)簽將portType中定義的 operation同SOAP的請(qǐng)求綁定,定義了操作名稱soapAction,輸出輸入?yún)?shù)和異常的編碼方式及命名空間。
WSDL服務(wù)地址綁定元素:< service >
- <service name="Example">
- <port name="ExamplePort" binding="tns:ExamplePortBinding">
- <soap:address location="http://localhost:8080/hello"></soap:address>
- </port>
- </service>
service是一套<port>元素。在一一對(duì)應(yīng)形式下,每個(gè)<port>元素都和一個(gè)location關(guān)聯(lián)。如果同一個(gè)<binding>有多個(gè)<port>元素與之關(guān)聯(lián),可以使用額外的URL地址作為替換。
一個(gè)WSDL文檔中可以有多個(gè)<service>元素,而且多個(gè)<service>元素十分有用,其中之一就是可以根據(jù)目標(biāo)URL來組織端口。在一個(gè) WSDL文檔中,<service>的name屬性用來區(qū)分不同的service。在同一個(gè)service中,不同端口,使用端口的"name"屬性區(qū) 分。
簡單的描述了WSDL對(duì)SOAP協(xié)議的支持,以及在Web Service中的作用。