WSDL (Web Services Description Language,Web服務描述語言)是一種XML Application,他將Web服務描述定義爲一組服務訪問點,客戶端能夠經過這些服務訪問點對包含面向文檔信息或面向過程調用的服務進行訪問(相似遠程過程調用)。WSDL首先對訪問的操做和訪問時使用的請求/響應消息進行抽象描述,而後將其綁定到具體的傳輸協議和消息格式上以最終定義具體部署的服務訪問點。相關的具體部署的服務訪問點經過組合就成爲抽象的Web服務。 本文將詳細講解WSDL文檔的結構,並分析每一個元素的做用。
一:WSDL定義
WSDL是一個用於精確描述Web服務的文檔,WSDL文檔是一個遵循WSDL XML模式的XML文檔。WSDL 文檔將Web服務定義爲服務訪問點或端口的集合。在 WSDL 中,因爲服務訪問點和消息的抽象定義已從具體的服務部署或數據格式綁定中分離出來,所以能夠對抽象定義進行再次使用:消息,指對交換數據的抽象描述;而端口類型,指操做的抽象集合。用於特定端口類型的具體協議和數據格式規範構成了能夠再次使用的綁定。將Web訪問地址與可再次使用的綁定相關聯,能夠定義一個端口,而端口的集合則定義爲服務。
一個WSDL文檔一般包含7個重要的元素,即types、import、message、portType、operation、binding、 service元素。這些元素嵌套在definitions元素中,definitions是WSDL文檔的根元素。文章的下一部分將會詳細介紹WSDL 的基本結構。
二:WSDL的基本結構--概述
如第一部分最後描述的那樣,一個基本的WSDL文檔包含7個重要的元素。下面將分別介紹這幾個元素以及他們的做用。
WSDL 文檔在Web服務的定義中使用下列元素:
· Types - 數據類型定義的容器,它使用某種類型系統(通常地使用XML Schema中的類型系統)。
· Message - 通訊消息的數據結構的抽象類型化定義。使用Types所定義的類型來定義整個消息的數據結構。
· Operation - 對服務中所支持的操做的抽象描述,通常單個Operation描述了一個訪問入口的請求/響應消息對。
· PortType - 對於某個訪問入口點類型所支持的操做的抽象集合,這些操做能夠由一個或多個服務訪問點來支持。
· Binding - 特定端口類型的具體協議和數據格式規範的綁定。
· Port - 定義爲協議/數據格式綁定與具體Web訪問地址組合的單個服務訪問點。
· Service- 相關服務訪問點的集合。
WSDL的xml schema能夠參照以下網址:http://schemas.xmlsoap.org/wsdl/
三:WSDL的基本結構--詳述
本節將經過一個例子詳細描述WSDL文檔每一個元素的做用。下面一個例子是一個簡單的WSDL文檔的內容,該文檔的產生能夠參見個人另一篇文章:xfire開發實例--HelloWorld篇 。
一個簡單的Web Service的WSDL文檔,該服務支持名爲sayHello的惟一操做,該操做經過在http上運行SOAP協議來實現的。該請求接受一個字符串name,通過處理後返回一個簡單的字符串。文檔以下:
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions
targetNamespace="http://com.liuxiang.xfireDemo/HelloService"
xmlns:tns="http://com.liuxiang.xfireDemo/HelloService"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"
xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://com.liuxiang.xfireDemo/HelloService">
<xsd:element name="sayHello">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1"
name="name" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1"
name="out" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse" />
</wsdl:message>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello" />
</wsdl:message>
<wsdl:portType name="HelloServicePortType">
<wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest"
message="tns:sayHelloRequest" />
<wsdl:output name="sayHelloResponse"
message="tns:sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloServiceHttpBinding"
type="tns:HelloServicePortType">
<wsdlsoap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloService">
<wsdl:port name="HelloServiceHttpPort"
binding="tns:HelloServiceHttpBinding">
<wsdlsoap:address
location="http://localhost:8080/xfire/services/HelloService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
♦ types元素使用XML模式語言聲明在WSDL文檔中的其餘位置使用的複雜數據類型與元素;
♦ import元素相似於XML模式文檔中的import元素,用於從其餘WSDL文檔中導入WSDL定義;
♦ message元素使用在WSDL文檔的type元素中定義或在import元素引用的外部WSDL文檔中定義的XML模式的內置類型、複雜類型或元素描述了消息的有效負載;
♦ portType元素和operation元素描述了Web服務的接口並定義了他的方法。portType元素和operation元素相似於 java接口和接口中定義的方法聲明。operation元素使用一個或者多個message類型來定義他的輸入和輸出的有效負載;
♦ Binding元素將portType元素和operation元素賦給一個特殊的協議和編碼樣式;
♦ service元素負責將Internet地址賦給一個具體的綁定;
一、definitions元素
全部的WSDL文檔的根元素均是definitions元素。該元素封裝了整個文檔,同時經過其name提供了一個WSDL文檔。除了提供一個命名空間外,該元素沒有其餘做用,故不做詳細描述。
下面的代碼是一個definitions元素的結構:
<wsdl:definitions
targetNamespace="http://com.liuxiang.xfireDemo/HelloService"
xmlns:tns="http://com.liuxiang.xfireDemo/HelloService"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"
xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
</wsdl:definitions>
二、types元素
WSDL採用了W3C XML模式內置類型做爲其基本類型系統。types元素用做一個容器,用於定義XML模式內置類型中沒有描述的各類數據類型。當聲明消息部分的有效負載時,消息定義使用了在types元素中定義的數據類型和元素。在本文的WSDL文檔中的types定義:
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://com.liuxiang.xfireDemo/HelloService">
<xsd:element name="sayHello">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1"
name="name" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1"
name="out" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
上面是數據定義部分,該部分定義了兩個元素,一個是sayHello,一個是sayHelloResponse:
sayHello:定義了一個複雜類型,僅僅包含一個簡單的字符串,未來用來描述操做的參入傳入部分;
sayHelloResponse:定義了一個複雜類型,僅僅包含一個簡單的字符串,未來用來描述操做的返回值;
三、import元素
import元素使得能夠在當前的WSDL文檔中使用其餘WSDL文檔中指定的命名空間中的定義元素。本例子中沒有使用import元素。一般在用戶但願模塊化WSDL文檔的時候,該功能是很是有效果的。
import的格式以下:
<wsdl:import namespace="http://xxx.xxx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/>
必須有namespace屬性和location屬性:
namespace屬性:值必須與正導入的WSDL文檔中聲明的targetNamespace相匹配;
location屬性:必須指向一個實際的WSDL文檔,而且該文檔不能爲空。
四、message元素
message元素描述了Web服務使用消息的有效負載。message元素能夠描述輸出或者接受消息的有效負載;還能夠描述SOAP文件頭和錯誤detail元素的內容。定義message元素的方式取決於使用RPC樣式仍是文檔樣式的消息傳遞。在本文中的message元素的定義,本文檔使用了採用文檔樣式的消息傳遞:
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse" />
</wsdl:message>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello" />
</wsdl:message>
該部分是消息格式的抽象定義:定義了兩個消息sayHelloResponse和sayHelloRequest:
sayHelloRequest:sayHello操做的請求消息格式,由一個消息片段組成,名字爲parameters,元素是咱們前面定義的types中的元素;
sayHelloResponse:sayHello操做的響應消息格式,由一個消息片段組成,名字爲parameters,元素是咱們前面定義的types中的元素;
若是採用RPC樣式的消息傳遞,只須要將文檔中的element元素應以修改成type便可。
五、portType元素
portType元素定義了Web服務的抽象接口。該接口有點相似Java的接口,都是定義了一個抽象類型和方法,沒有定義實現。在WSDL中, portType元素是由binding和service元素來實現的,這兩個元素用來講明Web服務實現使用的Internet協議、編碼方案以及 Internet地址。
一個portType中能夠定義多個operation,一個operation能夠看做是一個方法,本文中WSDL文檔的定義:
<wsdl:portType name="HelloServicePortType">
<wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest"
message="tns:sayHelloRequest" />
<wsdl:output name="sayHelloResponse"
message="tns:sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
portType定義了服務的調用模式的類型,這裏包含一個操做sayHello方法,同時包含input和output代表該操做是一個請求/響應模式,請求消息是前面定義的sayHelloRequest,響應消息是前面定義的sayHelloResponse。input表示傳遞到Web服務的有效負載,output消息表示傳遞給客戶的有效負載。
6、binding
binding元素將一個抽象portType映射到一組具體協議(SOAO和HTTP)、消息傳遞樣式、編碼樣式。一般binding元素與協議專有的元素和在一塊兒使用,本文中的例子:
<wsdl:binding name="HelloServiceHttpBinding"
type="tns:HelloServicePortType">
<wsdlsoap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</java