WSDL教程

您應當具備的基礎知識

在繼續學習之前,您需要對下面的知識有基本的瞭解:

  • XML

  • XML 命名空間

  • XML Schema

如果您希望首先學習這些項目,請訪問我們的 XML 系列教程

什麼是 WSDL?

  • WSDL 指網絡服務描述語言

  • WSDL 使用 XML 編寫

  • WSDL 是一種 XML 文檔

  • WSDL 用於描述網絡服務

  • WSDL 也可用於定位網絡服務

  • WSDL 還不是 W3C 標準

WSDL 可描述網絡服務(Web Services)

WSDL 指網絡服務描述語言 (Web Services Description Language)。

WSDL 是一種使用 XML 編寫的文檔。這種文檔可描述某個 Web service。它可規定服務的位置,以及此服務提供的操作(或方法)。


 

WSDL 文檔結構

WSDL 文檔是利用這些主要的元素來描述某個 web service 的:

元素 定義
<portType>web service 執行的操作
<message>web service 使用的消息
<types>web service 使用的數據類型
<binding>web service 使用的通信協議 

一個 WSDL 文檔的主要結構是類似這樣的:

<definitions>

<types>   definition of types........</types>

<message>   definition of a message....</message>

<portType>   definition of a port.......</portType>

<binding>   definition of a binding....</binding>

</definitions>

WSDL 文檔可包含其它的元素,比如 extension 元素,以及一個 service 元素,此元素可把若干個 web services 的定義組合在一個單一的 WSDL 文檔中。


WSDL 端口

<portType> 元素是最重要的 WSDL 元素。


它可描述一個 web service、可被執行的操作,以及相關的消息。


可以把 <portType> 元素比作傳統編程語言中的一個函數庫(或一個模塊、或一個類)。 


WSDL 消息

<message> 元素定義一個操作的數據元素。


每個消息均由一個或多個部件組成。可以把這些部件比作傳統編程語言中一個函數調用的參數。


WSDL types

<types> 元素定義 web service 使用的數據類型。


爲了最大程度的平臺中立性,WSDL 使用 XML Schema 語法來定義數據類型。



WSDL Bindings

<binding> 元素爲每個端口定義消息格式和協議細節。


WSDL 實例

這是某個 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>

在這個例子中,<portType> 元素把 "glossaryTerms" 定義爲某個端口的名稱,把 "getTerm" 定義爲某個操作的名稱。

操作 "getTerm" 擁有一個名爲 "getTermRequest" 的輸入消息,以及一個名爲 "getTermResponse" 的輸出消息

<message> 元素可定義每個消息的部件,以及相關聯的數據類型。

對比傳統的編程,glossaryTerms 是一個函數庫,而 "getTerm" 是帶有輸入參數 "getTermRequest" 和返回參數 getTermResponse 的一個函數。


WSDL 端口可描述由某個 web service 提供的界面(合法操作)。

WSDL 端口

<portType> 元素是最重要的 WSDL 元素。

它可描述一個 web service、可被執行的操作,以及相關的消息

端口定義了指向某個 web service 的連接點。可以把該元素比作傳統編程語言中的一個函數庫(或一個模塊、或一個類),而把每個操作比作傳統編程語言中的一個函數。



操作類型

請求-響應是最普通的操作類型,不過 WSDL 定義了四種類型:

類型定義
One-way此操作可接受消息,但不會返回響應。
Request-response此操作可接受一個請求並會返回一個響應 
Solicit-response此操作可發送一個請求,並會等待一個響應。
Notification此操作可發送一條消息,但不會等待響應。 

One-Way 操作

一個 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" 操作可接受新術語表項目消息的輸入,這些消息使用一條名爲 "newTermValues" 的消息,此消息帶有輸入參數 "term" 和 "value"。不過,沒有爲這個操作定義任何輸出。




Request-Response 操作

一個 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" 的參數,並將返回一個名爲 "getTermResponse" 的輸出消息,此消息帶有一個名爲 "value" 的參數。


WSDL 綁定可爲 web service 定義消息格式和協議細節。

綁定到 SOAP

一個 請求 - 響應 操作的例子:

<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 協議。在這個例子中我們使用 HTTP。

operation 元素定義了每個端口提供的操作符。

對於每個操作,相應的 SOAP 行爲都需要被定義。同時您必須如何對輸入和輸出進行編碼。在這個例子中我們使用了 "literal"。


UDDI 是一種目錄服務,企業可以使用它對 Web services 進行註冊和搜索。

UDDI,英文爲 "Universal Description, Discovery and Integration",可譯爲“通用描述、發現與集成服務”。

什麼是 UDDI?

UDDI 是一個獨立於平臺的框架,用於通過使用 Internet 來描述服務,發現企業,並對企業服務進行集成。

  • UDDI 指的是通用描述、發現與集成服務

  • UDDI 是一種用於存儲有關 web services 的信息的目錄。

  • UDDI 是一種由 WSDL 描述的 web services 界面的目錄。

  • UDDI 經由 SOAP 進行通信

  • UDDI 被構建入了微軟的 .NET 平臺

UDDI 基於什麼?

UDDI 使用 W3C 和 IETF* 的因特網標準,比如 XML、HTTP 和 DNS 協議。

UDDI 使用 WSDL 來描述到達 web services 的界面

此外,通過採用 SOAP,還可以實現跨平臺的編程特性,大家知道,SOAP 是 XML 的協議通信規範,可在 W3C 的網站找到相關的信息。

*註釋:IETF - Internet Engineering Task Force

UDDI 的好處

任何規模的行業或企業都能得益於 UDDI。

在 UDDI 之前,還不存在一種 Internet 標準,可以供企業爲它們的企業和夥伴提供有關其產品和服務的信息。也不存在一種方法,來集成到彼此的系統和進程中。

UDDI 規範幫助我們解決的問題:

  • 使得在成百萬當前在線的企業中發現正確的企業成爲可能

  • 定義一旦首選的企業被發現後如何啓動商業

  • 擴展新客戶並增加對目前客戶的訪問

  • 擴展銷售並延伸市場範圍

  • 滿足用戶驅動的需要,爲在全球 Internet 經濟中快速合作的促進來清除障礙

UDDI 如何被使用

假如行業發佈了一個用於航班比率檢測和預訂的 UDDI 標準,航空公司就可以把它們的服務註冊到一個 UDDI 目錄中。然後旅行社就能夠搜索這個 UDDI 目錄以找到航空公司預訂界面。當此界面被找到後,旅行社就能夠立即與此服務進行通信,這樣由於它使用了一套定義良好的預訂界面。

誰在支持 UDDI?

UDDI 是一個跨行業的研究項目,由所有主要的平臺和軟件提供商驅動,比如:Dell, Fujitsu, HP, Hitachi, IBM, Intel, Microsoft, Oracle, SAP, 以及 Sun, 它既是一個市場經營者的團體,也是一個電子商務的領導者。

已有數百家公司參與了這個 UDDI 團體。



WSDL語法

<wsdl:definitions name="nmtoken"? targetNamespace="uri">

    <import namespace="uri" location="uri"/> *
	
    <wsdl:documentation .... /> ?

    <wsdl:types> ?
        <wsdl:documentation .... /> ?
        <xsd:schema .... /> *
    </wsdl:types>

    <wsdl:message name="ncname"> *
        <wsdl:documentation .... /> ?
        <part name="ncname" element="qname"? type="qname"?/> *
    </wsdl:message>

    <wsdl:portType name="ncname"> *
        <wsdl:documentation .... /> ?
        <wsdl:operation name="ncname"> *
            <wsdl:documentation .... /> ?
            <wsdl:input message="qname"> ?
                <wsdl:documentation .... /> ?
            </wsdl:input>
            <wsdl:output message="qname"> ?
                <wsdl:documentation .... /> ?
            </wsdl:output>
            <wsdl:fault name="ncname" message="qname"> *
                <wsdl:documentation .... /> ?
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:serviceType name="ncname"> *
        <wsdl:portType name="qname"/> +
    </wsdl:serviceType>

    <wsdl:binding name="ncname" type="qname"> *
        <wsdl:documentation .... /> ?
        <-- binding details --> *
        <wsdl:operation name="ncname"> *
            <wsdl:documentation .... /> ?
            <-- binding details --> *
            <wsdl:input> ?
                <wsdl:documentation .... /> ?
                <-- binding details -->
            </wsdl:input>
            <wsdl:output> ?
                <wsdl:documentation .... /> ?
                <-- binding details --> *
            </wsdl:output>
            <wsdl:fault name="ncname"> *
                <wsdl:documentation .... /> ?
                <-- binding details --> *
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="ncname" serviceType="qname"> *
        <wsdl:documentation .... /> ?
        <wsdl:port name="ncname" binding="qname"> *
            <wsdl:documentation .... /> ?
            <-- address details -->
        </wsdl:port>
    </wsdl:service>

</wsdl:definitions>


總結


WSDL 概要

本教程已爲您講解了如何創建可描述 web 服務的 WSDL 文檔。它也規定了服務的位置和服務所提供的操作(或方法)。

您已經學習到如何爲 web 服務定義消息格式和協議細節。

您也學習了可通過 UDDI 來註冊和搜索 web 服務。


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