Web服務(wù)要素之一:WSDL
WSDL
1、WSDL文檔結(jié)構(gòu)
WSDL元素結(jié)構(gòu)示意圖如下圖所示:
其中:
1)Types是一個數(shù)據(jù)類型定義的容器,包含了所有在消息定義中需要的XML元素的類型定義。
2)Message具體定義了在通信中使用的消息的數(shù)據(jù)結(jié)構(gòu),Message元素包含了一組Part元素,每個Part元素都是最終消息的一個組成部分,每個Part都會引用一個DataType來表示它的結(jié)構(gòu)。Part元素不支持嵌套。
3)PortType具體定義了一種服務(wù)訪問入口的類型,何謂訪問入口的類型呢?就是傳入/傳出消息的模式及其格式。
一個PortType可以包含若干個Operation,而一個Operation則是指訪問入口支持的一種類型的調(diào)用。以上三種結(jié)構(gòu)描述了調(diào)用Web服務(wù)的抽象定義,這三部分與具體Web服務(wù)部署細節(jié)無關(guān),是可復用的描述(每個層次都可以復用)。
4)Service描述的是一個具體的被部署的Web服務(wù)所提供的所有訪問入口的部署細節(jié),一個Service往往會包含多個服務(wù)訪問入口,而每個訪問入口都會使用一個Port元素來描述。
5)Port描述的是一個服務(wù)訪問入口的部署細節(jié),包括通過哪個Web地址(URL)來訪問,應(yīng)當使用怎樣的消息調(diào)用模式來訪問等。其中消息調(diào)用模式則是使用Binding結(jié)構(gòu)來表示。
6)Binding結(jié)構(gòu)定義了某個PortType與某一種具體的網(wǎng)絡(luò)傳輸協(xié)議或消息傳輸協(xié)議相綁定,從這一層次開始,描述的內(nèi)容就與具體服務(wù)的部署相關(guān)了。比如可以將PortType與SOAP/HTTP綁定,也可以將PortType與MIME/SMTP相綁定等。
2、WSDL端口
元素是最重要的WSDL元素。
它可描述一個Web service可被執(zhí)行的操作以及相關(guān)的消息。
可以把元素比作傳統(tǒng)編程語言中的一個函數(shù)庫(或一個模塊,或一個類)。
端口包含如下類型:
1)一個One-way操作的例子:
<message name="newTermValues">
<part name="term" type="xs:string"/>
<part name="value" type="xs:string"/>
</message>
<portType name="glossaryTerms">
<operation name="setTerm">
<input name="newTerm" message="newTermValues"/>
</operation>
</portType >
在這個例子中,端口"glossaryTerms"定義了一個名為"setTerm"的one-way操作。
這個"setTerm"操作可接受新術(shù)語表項目消息的輸入,這些消息使用一條名為"newTermValues"的消息,此消息帶有輸入?yún)?shù)"term"和"value"。不過,沒有為這個操作定義任何輸出。
2)一個Request-response操作的例子:
<message name="getTermRequest">
<part name="term" type="xs:string"/>
</message>
<message name="getTermResponse">
<part name="value" type="xs:string"/>
</message>
<portType name="glossaryTerms">
<operation name="getTerm">
<input message="getTermRequest"/>
<output message="getTermResponse"/>
</operation>
</portType>
在這個例子中,端口“glossaryTerms”定義了一個名為“getTerm”的request-response操作。
“getTerm”操作會請求一個名為“getTermRequest”的輸入消息,此消息帶有一個名為“term”的參數(shù),并將返回一個名為 “getTermResponse”的輸出消息,此消息帶有一個名為“value”的參數(shù)。
3、WSDL綁定
一個綁定的例子:
<message name="getTermRequest">
<part name="term" type="xs:string"/>
</message>
<message name="getTermResponse">
<part name="value" type="xs:string"/>
</message>
<portType name="glossaryTerms">
<operation name="getTerm">
<input message="getTermRequest"/>
<output message="getTermResponse"/>
</operation>
</portType>
<binding type="glossaryTerms" name="b1">
<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>
binding元素有“name”和“type”兩個屬性。“name”屬性定義binding的名稱,而“type”屬性指向binding的端口,在這個例子中是“glossaryTerms”端口。
soap:binding元素有“style”和“transport”兩個屬性。“style”屬性可取值為“rpc”或“document”。
在這個例子中我們使用“document”。“transport”屬性定義SOAP使用的協(xié)議,在這個例子中使用HTTP。
operation元素定義了每個端口提供的操作符。對于每個操作,相應(yīng)的SOAP行為都需要被定義。同時必須知道如何對輸入和輸出進行編碼。在這個例子中使用了“l(fā)iteral”。