WSDL介紹web
介紹過了soap,下面再看看wsdl的介紹。
函數
下面是wsdl的主要文檔元素:編碼
WSDL文檔能夠分爲兩部分。頂部分由抽象定義組成,而底部分則由具體描述組成。抽象部分以獨立於平臺和語言的方式定義SOAP消息,它們並不包含任何隨機器或語言而變的元素。這就定義了一系列服務,大相徑庭的應用均可以實現。具體部分,如數據的序列化則納入底部分,由於它包含具體的定義。在上述的文檔元素中,<types>、<message>、<portType>屬於抽象定義層,<binding>、<service>屬於具體定義層。全部的抽象能夠是單獨存在於別的文件中,也能夠從主文檔中導入。 spa
WSDL文檔的結構實例解析code
下面咱們將經過一個實際的WSDL文檔例子來詳細說明各標籤的做用及關係。orm
<?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>定義了文檔中用到的各個xml元素的namespace縮寫,也界定了本文檔本身的targetNamespace="http://www.jsoso.com/wstest",這意味着其它的XML要引用當前XML中的元素時,要聲明這個namespace。注意xmlns:tns="http://www.jsoso.com/wstest"這個聲明,它標示了使用tns這個前綴指向自身的命名空間。element
WSDL文檔數據類型定義元素:<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>標籤訂義了當前的WSDL文檔用到的數據類型。要說明的是,爲了最大程度的平臺中立性,WSDL 使用 XML Schema 語法來定義數據類型。這些數據類型用來定義web service方法的參數和返回指。對於通用的原生數據類型如:integer , boolean , char , float等,在W3C的標準文檔http://www.w3.org/2001/XMLSchema中已經作了定義。這裏咱們要引入的schema定義schemaLocation="http://localhost:8080/hello?xsd=1"是咱們自定義的Java對象類型。
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函數的參數。<message>元素中的每一個<part>子元素都和某個參數相符。輸入參數在<message>元素中定義,與輸出參數相隔離,輸出參數有本身的<message>元素。兼做輸入、輸出的參數在輸入輸出的<message>元素中有它們相應的<part>元素。輸出<message>元素以"Response"結尾,對Java而言方法得返回值就對應一個輸出的<message>。每一個<part>元素都有名字和類型屬性,就像函數的參數有參數名和參數類型。
在上面的文檔中有兩個輸入參數、兩個輸出參數和一個錯誤參數(對應Java中的Exception)。
輸入參數<message>的name屬性分別命名爲toSayHello,sayHello。
toSayHello對應輸入參數userName,參數類型爲xsd:string,在Java語言中就是String;
sayHello對應兩個輸入參數person和arg1,類型爲tns:person和xsd:string。這裏tns:person類型就是引用了< types >標籤中的類型定義。
輸出參數<message>的name屬性分別命名爲toSayHelloResponse和sayHelloResponse。
這個名稱和輸入參數的<message>標籤name屬性對應,在其後面加上Response尾綴。
toSayHelloResponse對應的返回值是returnWord,參數類型爲xsd:string;
sayHelloResponse對應的返回值是personList,參數類型爲tns:personArray(自定義類型);
錯誤參數<message>的name屬性爲HelloException。
它的<part>子標籤element而不是type來定義類型。
以上的message標籤的name屬性一般使用web service函數方法名做爲參照,錯誤參數標籤則使用異常類名爲參照。標籤中的參數名稱,即part子元素的name屬性是可自定義的。message標籤的參數類型將引用types標籤的定義。
WSDL文檔函數體定義元素:< 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>
<portType> 元素是最重要的 WSDL 元素。它可描述一個 web service、可被執行的操做,以及相關的消息。portType的name屬性對應Java中的一個服務類的類名。<portType> 元素使用其子元素< operation>描述一個web service的服務方法。
在<operation>元素中,name屬性表示服務方法名,parameterOrder屬性表示方法的參數順序,使用空格符分割多個參數,如:「parameterOrder="person arg1」。<operation>元素的子標籤<input>表示輸入參數說明,它引用<message>標籤中的輸入參數。<output>表示輸出參數說明,它引用<message>標籤中的輸出參數。<fault>標籤在Java方法中的特別用來表示異常(其它語言有對應的錯誤處理機制),它引用<message>標籤中的錯誤參數。
WSDL綁定實現定義元素:< 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>標籤是完整描述協議、序列化和編碼的地方,<types>,<message>和<portType>標籤處理抽象的數據內容,而<binding>標籤是處理數據傳輸的物理實現。
<binding>標籤把前三部分的抽象定義具體化。
首先<binding>標籤使用<soap:binding>的transport和style屬性定義了Web Service的通信協議HTTP和SOAP的請求風格RPC。其次<operation>子標籤將portType中定義的operation同SOAP的請求綁定,定義了操做名稱soapAction,輸出輸入參數和異常的編碼方式及命名空間。
WSDL服務地址綁定元素:< service >
<service name="Example"> <port name="ExamplePort" binding="tns:ExamplePortBinding"> <soap:address location="http://localhost:8080/hello"></soap:address> </port> </service>
service是一套<port>元素。在一一對應形式下,每一個<port>元素都和一個location關聯。若是同一個<binding>有多個<port>元素與之關聯,可使用額外的URL地址做爲替換。
一個WSDL文檔中能夠有多個<service>元素,並且多個<service>元素十分有用,其中之一就是能夠根據目標URL來組織端口。在一個WSDL文檔中,<service>的name屬性用來區分不一樣的service。在同一個service中,不一樣端口,使用端口的"name"屬性區分。
以上的內容是介紹了webservice的構成結構以及結構的概念,理解了這些概念後,咱們帶着這些理解去編寫一些代碼,讓咱們用代碼(另外一個女友)來和咱們的理解一一應證!(代碼虐我千百遍,我待代碼如初戀!)