一、WSDL概述
        WebServices Description Language (WSDL Web服務(wù)語言)是一個用于精確描述Web Service的文檔格式。
        WSDL非常適合于用作代碼生成器,它能夠讀取WSDL文檔,并且可以為訪問Web服務(wù)生成一個程序化的接口,大多數(shù)軟件供應(yīng)商和主要的標(biāo)準(zhǔn)機(jī)構(gòu)(包括 W3C、WS-I和OASIS)都支持WSDL。例如:JAX-RPC provider(例如:BEA Weblogic)通過API用WSDL生成相應(yīng)的占位程序;IBM WebSphere、Microsoft.NET以及Apache Axis都有自己的工具生成相關(guān)的代碼。下圖是一個例子:
                                      

       上面的例子JAX-RPC通過讀取WSDL文檔,創(chuàng)建JAX-RPC RMI接口(endpoint接口)和實現(xiàn)此接口的網(wǎng)絡(luò)占位程序(stub)。客戶端程序通過RMI接口,Stub和Web Service服務(wù)端交換SAOP消息。

二、WSDL基本結(jié)構(gòu)
        WSDL文檔是一個遵循WSDL XML模式的XML文檔(文檔實例);類似于:SOAP文檔是一個遵循SOAP XML模式的XML文檔(文檔實例);
        一個WSDL文檔的根元素是definitions元素,WSDL文檔包含7個重要的元素:types, import, message, portType, operations, binding和service元素。

三、WSDL聲明
      
        3.1 XML聲明

           <?xml version="1.0" encoding="UTF-8"?>

            WSDL的聲明必須定義成使用:UTF-8 或者UTF-16 編碼。

        3.2 definition元素
              所有WSDL文檔的根元素都是definition元素。   

<definitions name="BookQuoteWS"
                      targetNamespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:mh
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:soapbind
="http://schemas.xmlsoap.org/wsdl/soap/"
                         xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
                     xmlns
="http://schemas.xmlsoap.org/wsdl/">

 definition元素中一般包括若干個XML命名空間;
 http://schemas.xmlsoap.org/wsdl/是默認(rèn)的命名空間,這樣就可以不用顯式地定義每一個WSDL元素的命名空間了,例如:
<types> <messages> <portType>…;文檔中所有的元素缺省應(yīng)該屬于這個命名空間。
definition元素的的一個屬性是name,此屬性不重要可以沒有;
   定義了targetNamespace命名空間,它為在模式中顯式創(chuàng)建的所有新類型均聲明了XML命名空間,而且上面的例子中賦予了mh前綴。

<!-- message elements describe the input and output parameters -->

<message name="GetBookPriceRequest">

      
<part name="isbn" type="xsd:string" />

</message>

<message name="GetBookPriceResponse">

    
<part name="price" type="xsd:float" />

</message>

<!-- portType element describes the abstract interface of a Web service -->

<portType name="BookQuote">

   
<operation name="getBookPrice">

         
<input name="isbn" message="mh:GetBookPriceRequest"/>

         
<output name="price" message="mh:GetBookPriceResponse"/>

  
</operation>

</portType>

 

         上面的例子中:message元素利用name屬性指定了標(biāo)簽(例如:GetBookPriceRequest),這些標(biāo)簽會自動使用targetNamespace的命名空間,標(biāo)簽了的messages元素通常被稱為定義。
          文檔中的其他元素用標(biāo)簽和命名空間前綴去應(yīng)用定義,例如上面的例子中:input元素是使用mh:GetBookPriceRequest來引用標(biāo)簽GetBookPriceRequest。

         3.3 Types元素
               Types元素用作一個容器,定義了自定義的特殊數(shù)據(jù)類型,在聲明消息部分(有效負(fù)載)的時候,messages定義使用了types元素中定義的數(shù)據(jù)類型與元素。

<?xml version="1.0" encoding="UTF-8"?>

<definitions name="BookQuoteWS"
                      targetNamespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:mh
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:soapbind
="http://schemas.xmlsoap.org/wsdl/soap/"
                      xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
                     xmlns
="http://schemas.xmlsoap.org/wsdl/">
<types>

    
<xsd:schema   targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote">

      
<!-- The ISBN simple type -->

      
<xsd:simpleType name="ISBN">

        
<xsd:restriction base="xsd:string">

          
<xsd:pattern value="[0-9]{9}[0-9Xx]" />

        
</xsd:restriction>

      
</xsd:simpleType>

    
</xsd:schema>

</types>

         Types元素作為一個容器,用來定義XML模式內(nèi)置的數(shù)據(jù)類型(即復(fù)雜類型和定制的簡單類現(xiàn),詳細(xì)見Web Service XML文章)中沒有描述的各種數(shù)據(jù)類型。例如:ISBN。
        上面的例子中,types元素中直接嵌套了一個完整的W3C XML模式文檔,此文檔中targetNamespace必須是一個有效的非空值,而且必須屬于由WSDL文檔。

      3.4 Import元素
            Import元素可以讓當(dāng)前的文檔使用其他WSDL文檔中指定命名空間中的定義。 

<definitions name="AllMhWebServices"
         xmlns
="http://schemas.xmlsoap.org/wsdl/">

    
<import namespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"

     location
="http://www.Monson-Haefel.com/jwsbook/BookPrice.wsdl"/>

    
<import namespace="http://www.Monson-Haefel.com/jwsbook/po"

     location
="http://www.Monson-Haefel.com/jwsbook/wsdl/PurchaseOrder.wsdl"/>

    
<import namespace="http://www.Monson-Haefel.com/jwsbook/Shipping"

     location
="http://www.Monson-Haefel.com/jwsbook/wsdl/Shipping.wsdl"/>

</definitions >

          WSDL的import元素必須聲明兩個屬性,即namespace屬性和location屬性。
          namespace屬性必須和正導(dǎo)入的WSDL文檔中聲明的targetNamespace相匹配。
          location屬性必須指向一個實際的WSDL文檔。

四、WSDL抽象接口
         Message、portType和operation元素用于描述Web服務(wù)的抽象接口,相當(dāng)于JAVA或者C++中編程中的類的接口。其中portType相當(dāng)于類接口的名稱;operation相當(dāng)于接口中包含的函數(shù),message相當(dāng)于函數(shù)的參數(shù)和返回值。
       
        4.1 Message元素
              Message元素描述了Web服務(wù)的有效負(fù)載。相當(dāng)于函數(shù)調(diào)用中的參數(shù)和返回值。

<message name="GetBulkBookPriceRequest">

    
<part name="isbn" type="xsd:string"/>

    
<part name="quantity" type="xsd:int"/>

  
</message>

  
<message name="GetBulkBookPriceResponse">

    
<part name="price" type="mh:prices" />

  
</message>

RPC式樣的Web服務(wù)的message服務(wù)

GetBulkBookPriceRequest表示消息的輸入(相當(dāng)于函數(shù)的參數(shù)),GetBulkBookPriceResponse表示消息的輸出(相當(dāng)于函數(shù)的返回值)

Web Service的輸入和輸出參數(shù)可以是多個(一個特點),每一個輸入或者輸出使用part元素定義,RPC樣式必須使用type來定義類型

RPC樣式用類型來數(shù)據(jù)定義過程調(diào)用,調(diào)用中的每一個元素表示某一個類型的參數(shù)。

<types>

    
<xsd:schema targetNamespace="http://www.Monson-Haefel.com/jwsbook/PO">

      
<!-- Import the PurchaseOrder XML schema document -->

      
<xsd:import namespace="http://www.Monson-Haefel.com/jwsbook/PO"

       schemaLocation
="http://www.Monson-Haefel.com/jwsbook/po.xsd" />

    
</xsd:schema>

  
</types>

  
<!-- message elements describe the input and output parameters -->

  
<message name="SubmitPurchaseOrderMessage">

    
<part name="order" element="mh:purchaseOrder" />

  
</message>

文檔式樣Web服務(wù)的Messages元素:

當(dāng)用戶采用文檔式樣消息傳遞模式的時候,messages元素要應(yīng)用types定義中的頂級元素。具體頂級元素的定義和XML schema詳見Web Server XML文檔。

消息部分使用element屬性定義

文檔式樣的消息傳遞要交換XML文檔,并且應(yīng)用它們的頂級元素。

注:Messages元素的RPC/Document試樣對應(yīng)了SOAP RPC/Document消息傳遞模式,詳細(xì)見Web Server SOAP相關(guān)文檔

<types>

    
<xsd:schema targetNamespace="http://www.Monson-Haefel.com/jwsbook/PO">

      
<!-- Import the PurchaseOrder XML schema document -->

      
<xsd:element name="InvalidIsbnFaultDetail" >

        
<xsd:complexType>

          
<xsd:sequence>

            
<xsd:element name="offending-value" type="xsd:string"/>

            
<xsd:element name="conformance-rules" type="xsd:string" />

          
</xsd:sequence>

        
</xsd:complexType>

      
</xsd:element>

    
</xsd:schema>

  
</types>

 

  
<!-- message elements describe the input and output parameters -->

  
<message name="GetBookPriceRequest">

    
<part name="isbn" type="xsd:string" />

  
</message>

  
<message name="GetBookPriceResponse">

    
<part name="price" type="xsd:float" />

  
</message>

  
<message name="InvalidArgumentFault">

    
<part name="error_message" element="mh:InvalidIsbnFaultDetail" />

  
</message>

 

聲明錯誤消息:

錯誤使用的消息定義只能采用Document/Literal編碼樣式

上面聲明了匿名類型,InvalidIsbnFaultDetail不需要type類型,complexType中也不包括name屬性,詳細(xì)見Web Service XML相關(guān)文檔。


       4.2 portType元素
             PortType元素定義了Web服務(wù)的抽象接口,它可以由一個或者多個operation元素,每個operation元素定義了一個RPC樣式或者文檔樣式的Web服務(wù)方法。

       4.3 operation元素
            Operation元素要用一個或者多個messages消息來定義它的輸入、輸出以及錯誤。

<message name="GetBulkBookPriceRequest">

  
<part name="isbn" type="xsd:string"/>

  
<part name="quantity" type="xsd:int"/>

</message>

<message name="GetBulkBookPriceResponse">

  
<part name="prices" type="mh:prices" />

</message>

<message name="InvalidArgumentFault">

    
<part name="error_message" element="mh:InvalidIsbnFaultDetail" />

  
</message>

<portType name="GetBulkBookPrice" >

  
<operation name="getBulkBookPrice" parameterOrder="isbn quantity">

     
<input name="request" message="mh:GetBulkBookPriceRequest"/>

     
<output name="prices" message="mh:GetBulkBookPriceResponse"/>

<fault name="InvalidArgumentFault" message="mh:InvalidArgumentFault"/>

  
</operation>

</portType>

Input表示傳遞到Web服務(wù)的有效負(fù)載;output表示返回給客戶的有效負(fù)載;可以不包括,也可以包括一個或者多個fault錯誤消息。

parameterOrder定義了input和output消息采用的正確的順序

使用parameterOrder的時候,必須包含所有輸入?yún)?shù)部分;并且只包含不是返回類型的輸出部分,如果output只有一個part(上例),會假設(shè)返回值,所以不包括在parameterOrder中

如果parameterOrder列出output中的part部分,那么這個將被作為OUT參數(shù),如果input元素和output元素使用相同的名稱聲明了一個部分的時候,此部分為INOUT參數(shù)

        4.4 WSDL消息交換模式(MEP)
              Messaging Exchange Patterns(MEP)
              Web服務(wù)中使用了四種消息交換模式,即請求/響應(yīng)、單向、通知以及懇求/響應(yīng)模式。大多數(shù)基于WSDL的web服務(wù)使用請求/響應(yīng)和單向兩種模式。
              WSDL通過operation元素的input/output來定義使用那種模式,如果有input+output+可選的fault參數(shù),那就使用請求/響應(yīng)模式;如果只使用input,那就使用單向模式。
              在通知模式中:Web服務(wù)將消息發(fā)送給客戶,但不等待回復(fù);一般客戶通過注冊來接收通知;在懇求/響應(yīng)模式中類似通知模式,唯一的區(qū)別要期待客戶對Web服務(wù)的響應(yīng)。

五、WSDL實現(xiàn):binding元素
        Binding元素將一個抽象的portType映射到一組具體的協(xié)議(SOAP或者HTTP)、消息傳遞樣式(RPC或者document)以及編碼樣式(literal或者SOAP encoding)。
        Binding的類似于將接口或者函數(shù)的調(diào)用綁定到某種協(xié)議上:例如CORBA、COM或者RPC的方式,這里使用SOAP協(xié)議。

        5.1 soapbind:binding元素

<binding name="BookPrice_Binding" type="mh:BookQuote">

  
<soapbind:binding style="rpc"

   transport
="http://schemas.xmlsoap.org/soap/http"/>

  
<operation name="getBookPrice">

soapbind:binding元素指定了用于傳輸SOAP消息的Internet協(xié)議以及operation缺省的消息類型(RPC還是文檔類型)

http://schemas.xmlsoap.org/soap/http表示采用的是HTTP的傳輸方式,當(dāng)然也可以用HTTPS,用戶具體使用HTTP還是HTTPS取決于Port元素中定義的location屬性聲明中的模式。

上面的rpc表示缺省狀態(tài)下:operation將采用RPC的方式傳遞消息負(fù)載。

         5.2 soapbind:operation元素
<operation name="getBookPrice">

    
<soapbind:operation style="rpc"

     soapAction
=

     "http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice"
/>

POST 1ed/BookQuote HTTP/1.1

Host: www.Monson-Haefel.com

Content-Type: text/xml; charset="utf-8"

Content-Length: nnnn

SOAPAction="http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice"

soapbind:operation元素指定了消息傳遞樣式(RPC或者document),并且指定了SOAPAction字段的值。

上面的例子顯示在HTTP消息中的SOAPAction中對應(yīng)的值

       5.3 soapbind:body元素
<operation name="getBookPrice">

<soapbind:operation style="rpc"/>

<input>

          
<soapbind:body use="literal"

          namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

       
</input>

       
<output>

          
<soapbind:body use="literal"

          namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

       
</output>

<operation name="submit">

  
<soapbind:operation style="document"/>

      
<input>

        
<soapbind:body use="literal" />

      
</input>

      
<output>

        
<soapbind:body use="literal" />

      
</output>

</operation>

soapbind:body元素有四個屬性use、namespace、part和encodingStyle

對于WS-I use的屬性值必須是literal,意味這不是用編碼的方式,所以永遠(yuǎn)不會用到encodingStyle屬性

在RPC樣式中,必須用一個有效的URI指定的namespace屬性。此URI可以于WSDL文檔的targetNampspce相同;而在document樣式中不能使用namespace,XML文檔樣式的命名空間派生于它的XML文檔

          5.4 soapbind:fault元素
<fault name="InvalidArgumentFault">

     
<soapbind:fault name="InvalidArgumentFault" use="literal" />

</fault>

<portType name="BookQuote">

  
<operation name="getBookPrice">

     
<input name="isbn" message="mh:GetBookPriceRequest"/>

     
<output name="price" message="mh:GetBookPriceResponse"/>

     
<fault name="InvalidArgumentFault" message="mh:InvalidArgumentFault"/>

  
</operation>

</portType>

soapbind:fault元素和fault元素包含一個強制性的name屬性,表示要引用聲明于對應(yīng)portType中的專有錯誤消息

          5.5 soapbind:header元素
<types>

<xsd:schema 

targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"

     xmlns
="http://www.w3.org/2001/XMLSchema">

        
<xsd:element name="message-id" type="string" />

    
</xsd:schema>

</types>

 

<!-- message elements describe the input and output parameters -->

  
<message name="Headers">

    
<part name="message-id" element="mh:message-id" />

  
</message>

 

<operation name="getBookPrice">

  
<input>

     
<soapbind:header message="mh:Headers" part="message-id" use="literal" />

     
<soapbind:body use="literal"

           namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

        
</input>

WSDL在綁定的input元素、output元素中利用soapbind:header元素顯式指定了一個SOAP頭文件

        5.6 soapbind:headerfault元素  
<!-- message elements describe the input and output parameters -->

  
<message name="HeaderFault">

    
<part name="faultDetail" element="mh:detailMessage" />

  
</message>

 

<input>

      
<soapbind:header message="mh:Header" use="literal">

         
<soapbind:headerfault message="mh:Headers" use="literal" />

      
</soapbind:header>

      
<soapbind:body use="literal"

           namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

   
</input>

soapbind:headerfault元素表述了Header專用的錯誤消息,如果有一個響應(yīng)消息,必須在消息的Header元素中返回各種header的專用錯誤。

SOAP沒有就如何提供Header錯誤方面給出詳細(xì)說明,只是要求必須在Header元素中包含detail元素。有些SOAP工具箱將SOAP的fault放在header元素中。

六、WSDL實現(xiàn):Service和Port元素
<service name="BookPriceService">

  
<port name="BookPrice_Port" binding="mh:BookPrice_Binding">

    
<soapbind:address location=

     "http://www.Monson-Haefel.com/jwsbook/BookQuote"
 />

  
</port>

  
<port name="BookPrice_Failover_Port" binding="mh:BookPrice_Binding">

    
<soapbind:address location=

     "http://www.monson-haefel.org/jwsbook/BookPrice"
 />

  
</port>

  
<port name="SubmitPurchaseOrder_Port"

   binding
="mh:SubmitPurchaseOrder_Binding">

    
<soapbind:address location=

     "https://www.monson-haefel.org/jwsbook/po"
 />

  
</port>

</service>

Service元素包含一個或者多個Port元素

每一個Port元素對應(yīng)一個不同的Web服務(wù),port將一個URL賦予一個特定的binding,通過location實現(xiàn)

可以使兩個或者多個port元素將不同的URL賦給相同的binding,例如負(fù)載平衡和容錯的時候,使用這種方法。

soapbind:address:將Internet地址通過location屬性賦予一個SOAP綁定。