恩,我想說的是,是不是經常有人在開發的時候,特別是和第三方有接口的時候,走的是SOAP協議,然後用戶給你一個WSDL文件,說按照上面的進行適配,嘿嘿,這個時候,要是你以前沒有開發過,肯定會傻眼,那如果你想學習的話,就認真的看下面的講解咯:
一、WSDL概述
WebServices Description Language (WSDL Web服務語言)是一個用於精確描述Web Service的文檔格式。
WSDL非常適合於用作代碼生成器,它能夠讀取WSDL文檔,並且可以爲訪問Web服務生成一個程序化的接口,大多數軟件供應商和主要的標準機構(包括 W3C、WS-I和OASIS)都支持WSDL。例如:JAX-RPC provider(例如:BEA Weblogic)通過API用WSDL生成相應的佔位程序;IBM WebSphere、Microsoft.NET以及Apache Axis都有自己的工具生成相關的代碼。下圖是一個例子:
二、WSDL基本結構
WSDL文檔是一個遵循WSDL XML模式的XML文檔(文檔實例);類似於:SOAP文檔是一個遵循SOAP XML模式的XML文檔(文檔實例);
一個WSDL文檔的根元素是definitions元素,WSDL文檔包含7個重要的元素:types, import, message, portType, operations, binding和service元素。
三、WSDL聲明
3.1 XML聲明
WSDL的聲明必須定義成使用:UTF-8 或者UTF-16 編碼。
3.2 definition元素
所有WSDL文檔的根元素都是definition元素。
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/是默認的命名空間,這樣就可以不用顯式地定義每一個WSDL元素的命名空間了,例如:<types> <messages> <portType>…;文檔中所有的元素缺省應該屬於這個命名空間。
definition元素的的一個屬性是name,此屬性不重要可以沒有;
定義了targetNamespace命名空間,它爲在模式中顯式創建的所有新類型均聲明瞭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屬性指定了標籤(例如:GetBookPriceRequest),這些標籤會自動使用targetNamespace的命名空間,標籤了的messages元素通常被稱爲定義。
文檔中的其他元素用標籤和命名空間前綴去應用定義,例如上面的例子中:input元素是使用mh:GetBookPriceRequest來引用標籤GetBookPriceRequest。
3.3 Types元素
Types元素用作一個容器,定義了自定義的特殊數據類型,在聲明消息部分(有效負載)的時候,messages定義使用了types元素中定義的數據類型與元素。
<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模式內置的數據類型(即複雜類型和定製的簡單類現,詳細見Web Service XML文章)中沒有描述的各種數據類型。例如:ISBN。
上面的例子中,types元素中直接嵌套了一個完整的W3C XML模式文檔,此文檔中targetNamespace必須是一個有效的非空值,而且必須屬於由WSDL文檔。
3.4 Import元素
Import元素可以讓當前的文檔使用其他WSDL文檔中指定命名空間中的定義。
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屬性必須和正導入的WSDL文檔中聲明的targetNamespace相匹配。
location屬性必須指向一個實際的WSDL文檔。
四、WSDL抽象接口
Message、portType和operation元素用於描述Web服務的抽象接口,相當於JAVA或者C++中編程中的類的接口。其中portType相當於類接口的名稱;operation相當於接口中包含的函數,message相當於函數的參數和返回值。
4.1 Message元素
Message元素描述了Web服務的有效負載。相當於函數調用中的參數和返回值。
<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服務的message服務
GetBulkBookPriceRequest表示消息的輸入(相當於函數的參數),GetBulkBookPriceResponse表示消息的輸出(相當於函數的返回值)
Web Service的輸入和輸出參數可以是多個(一個特點),每一個輸入或者輸出使用part元素定義,RPC樣式必須使用type來定義類型
RPC樣式用類型來數據定義過程調用,調用中的每一個元素表示某一個類型的參數。
<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服務的Messages元素:
當用戶採用文檔式樣消息傳遞模式的時候,messages元素要應用types定義中的頂級元素。具體頂級元素的定義和XML schema詳見Web Server XML文檔。
消息部分使用element屬性定義
文檔式樣的消息傳遞要交換XML文檔,並且應用它們的頂級元素。
注:Messages元素的RPC/Document試樣對應了SOAP RPC/Document消息傳遞模式,詳細見Web Server SOAP相關文檔
<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屬性,詳細見Web Service XML相關文檔。
4.2 portType元素
PortType元素定義了Web服務的抽象接口,它可以由一個或者多個operation元素,每個operation元素定義了一個RPC樣式或者文檔樣式的Web服務方法。
4.3 operation元素
Operation元素要用一個或者多個messages消息來定義它的輸入、輸出以及錯誤。
<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服務的有效負載;output表示返回給客戶的有效負載;可以不包括,也可以包括一個或者多個fault錯誤消息。
parameterOrder定義了input和output消息採用的正確的順序
使用parameterOrder的時候,必須包含所有輸入參數部分;並且只包含不是返回類型的輸出部分,如果output只有一個part(上例),會假設返回值,所以不包括在parameterOrder中
如果parameterOrder列出output中的part部分,那麼這個將被作爲OUT參數,如果input元素和output元素使用相同的名稱聲明瞭一個部分的時候,此部分爲INOUT參數
4.4 WSDL消息交換模式(MEP)
Messaging Exchange Patterns(MEP)
Web服務中使用了四種消息交換模式,即請求/響應、單向、通知以及懇求/響應模式。大多數基於WSDL的web服務使用請求/響應和單向兩種模式。
WSDL通過operation元素的input/output來定義使用那種模式,如果有input+output+可選的fault參數,那就使用請求/響應模式;如果只使用input,那就使用單向模式。
在通知模式中:Web服務將消息發送給客戶,但不等待回覆;一般客戶通過註冊來接收通知;在懇求/響應模式中類似通知模式,唯一的區別要期待客戶對Web服務的響應。
五、WSDL實現:binding元素
Binding元素將一個抽象的portType映射到一組具體的協議(SOAP或者HTTP)、消息傳遞樣式(RPC或者document)以及編碼樣式(literal或者SOAP encoding)。
Binding的類似於將接口或者函數的調用綁定到某種協議上:例如CORBA、COM或者RPC的方式,這裏使用SOAP協議。
5.1 soapbind:binding元素
<soapbind:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getBookPrice">
soapbind:binding元素指定了用於傳輸SOAP消息的Internet協議以及operation缺省的消息類型(RPC還是文檔類型)
http://schemas.xmlsoap.org/soap/http表示採用的是HTTP的傳輸方式,當然也可以用HTTPS,用戶具體使用HTTP還是HTTPS取決於Port元素中定義的location屬性聲明中的模式。
上面的rpc表示缺省狀態下:operation將採用RPC的方式傳遞消息負載。
5.2 soapbind:operation元素
<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中對應的值
5.3 soapbind:body元素
<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,意味這不是用編碼的方式,所以永遠不會用到encodingStyle屬性
在RPC樣式中,必須用一個有效的URI指定的namespace屬性。此URI可以於WSDL文檔的targetNampspce相同;而在document樣式中不能使用namespace,XML文檔樣式的命名空間派生於它的XML文檔
5.4 soapbind:fault元素
<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屬性,表示要引用聲明於對應portType中的專有錯誤消息
5.5 soapbind:header元素
<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 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專用的錯誤消息,如果有一個響應消息,必須在消息的Header元素中返回各種header的專用錯誤。
SOAP沒有就如何提供Header錯誤方面給出詳細說明,只是要求必須在Header元素中包含detail元素。有些SOAP工具箱將SOAP的fault放在header元素中。
六、WSDL實現:Service和Port元素
<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元素對應一個不同的Web服務,port將一個URL賦予一個特定的binding,通過location實現
可以使兩個或者多個port元素將不同的URL賦給相同的binding,例如負載平衡和容錯的時候,使用這種方法。
soapbind:address:將Internet地址通過location屬性賦予一個SOAP綁定。