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 xml模式的xml文檔(文檔執行個體);類似于:soap文檔是一個遵循soap xml模式的xml文檔(文檔執行個體); 一個wsdl文檔的根元素是definitions元素,wsdl文檔包含7個重要的元素:types, import, message, porttype, operations, binding和service元素。
<?xml version="1.0" encoding="utf-8"?>
wsdl的聲明必須定義成使用:utf-8 或者utf-16 編碼。
所有wsdl文檔的根元素都是definition元素。
<definitions name="bookquotews"
xmlns:soapbind="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<!-- 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" />
<!-- 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。
types元素用作一個容器,定義了自定義的特殊資料類型,在聲明消息部分(有效負載)的時候,messages定義使用了types元素中定義的資料類型與元素。
<?xml version="1.0" encoding="utf-8"?>
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文檔。
import元素可以讓目前的文檔使用其他wsdl文檔中指定命名空間中的定義。
<definitions name="allmhwebservices"
xmlns="http://schemas.xmlsoap.org/wsdl/">
location="http://www.monson-haefel.com/jwsbook/bookprice.wsdl"/>
location="http://www.monson-haefel.com/jwsbook/wsdl/shipping.wsdl"/>
</definitions >
wsdl的import元素必須聲明兩個屬性,即namespace屬性和location屬性。 namespace屬性必須和正導入的wsdl文檔中聲明的targetnamespace相比對。 location屬性必須指向一個實際的wsdl文檔。
message、porttype和operation元素用于描述web服務的抽象接口,相當于java或者c++中程式設計中的類的接口。其中porttype相當于類接口的名稱;
operation相當于接口中包含的函數,message相當于函數的參數和傳回值。
message元素描述了web服務的有效負載。相當于函數調用中的參數和傳回值。
<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" />
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 -->
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" />
文檔式樣web服務的messages元素: 當使用者采用文檔式樣消息傳遞模式的時候,messages元素要應用types定義中的頂級元素。具體頂級元素的定義和xml schema詳見web server xml文檔。 消息部分使用element屬性定義 文檔式樣的消息傳遞要交換xml文檔,并且應用它們的頂級元素。 注:messages元素的rpc/document試樣對應了soap rpc/document消息傳遞模式,詳細見web server soap相關文檔
<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 name="getbookpricerequest">
<part name="isbn" type="xsd:string" />
<part name="price" type="xsd:float" />
<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服務方法。
operation元素要用一個或者多個messages消息來定義它的輸入、輸出以及錯誤。
<message name="getbulkbookpricerequest">
<part name="isbn" type="xsd:string"/>
<part name="quantity" type="xsd:int"/>
<message name="getbulkbookpriceresponse">
<part name="prices" type="mh:prices" />
<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)
wsdl通過operation元素的input/output來定義使用那種模式,如果有input+output+可選的fault參數,那就使用請求/響應模式;如果隻使用input,那就使用單向模式。 在通知模式中:web服務将消息發送給客戶,但不等待回複;一般客戶通過注冊來接收通知;在懇求/響應模式中類似通知模式,唯一的差別要期待客戶對web服務的響應。
<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 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綁定。
特别說明:尊重作者的勞動成果,轉載請注明出處哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt234