天天看點

WEBSERVICE入門03:WSDL結構詳解

本次主要是WSDL的詳解

在上一篇中,當我們在浏覽器中輸入http://localhost:6789/ns?wsdl時,我們就得到了一個WSDL檔案。

1.WSDL定義

WSDL(Web服務描述語言,WebService Description Language)是為描述Web服務釋出的XML格式。WSDL是一種XML Application,它将Web服務描述定義為一組服務通路點,用戶端可以通過這些服務通路點對包含面向文檔資訊或者面向過程調用的服務進行通路(類似遠端過程調用)。WSDL首先對通路的操作和通路時使用的請求/響應消息進行抽象描述,然後将其綁定到具體的傳輸協定和消息格式上以确定最終定義具體部署的服務通路點。相關的具體部署的服務通路點通過組合成為抽象的Web服務。

WSDL文檔将web服務定義為服務通路點或端口的集合。在WSDL中,由于通路服務點和消息的抽象定義已從具體的服務部署或資料格式綁定中分離出來,是以可以對抽象定義再次使用:消息,是指對交換資料的抽象描述;端口類型,是指操作的抽象集合。用于特定端口類型的具體協定和資料格式規範構成了可以再次使用的綁定。将Web通路位址與可再次使用的綁定想關聯,可以定義一個端口,而端口的集合則定義為服務。

2.WSDL概述

一個WSDL文檔通常包含7個重要的元素,即types、import、message、portType、operation、binding、service元素。這些元素嵌套在definitions元素中,definitions是WSDL文檔的根元素。

每個元素的基本含義如下:

Types:資料定義的容器,它使用某種系統類型(一般都是XMLSchema中的類型系統)

Message:通信消息的資料結構的抽象化定義。使用Types所定義的類型來定義整個消息的資料結構。

Operation:對服務中所支援的操作的抽象描述,一般單個Operation描述了一個通路入口的請求/響應位址

PortType:對于某個通路入口點類型所支援的操作的抽象集合,這些操作可以有一個或者多個服務通路點來支援。

Binding:特定端口類型的具體協定和資料格式規範的綁定。

Port:定義為協定/資料格式綁定與具體Web通路位址組合的單個服務通路點。

Service:相關服務通路點的集合。

3.WDSL示例詳解

輸入了http://localhost:6789/ns?wsdl(是基于上一篇釋出的服務的url)後,得到的檔案如下:

<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.wyj.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.wyj.com/" name="MyServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.wyj.com/" schemaLocation="http://localhost:6789/ns?xsd=1"/>
</xsd:schema>
</types>
<message name="add">
<part name="parameters" element="tns:add"/>
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse"/>
</message>
<message name="sayHi">
<part name="parameters" element="tns:sayHi"/>
</message>
<message name="sayHiResponse">
<part name="parameters" element="tns:sayHiResponse"/>
</message>
<portType name="IMyservice">
<operation name="add">...</operation>
<operation name="sayHi">...</operation>
</portType>
<binding name="MyServiceImplPortBinding" type="tns:IMyservice">...</binding>
<service name="MyServiceImplService">
<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://localhost:6789/ns"/>
</port>
</service>
</definitions>
           

這是一個簡單的web服務的WSDL文檔,該服務支援add和sayHi兩個操作,該操作通過在http上運作SOAP協定來實作的。其中add方法接受2個int類型的參數,傳回它們的和。而sayHi接受一個字元串,然後傳回一個簡單的字元串。其中:

types元素使用XML模式語言聲明在WSDL文檔中的其他位置使用的複雜資料類型與元素;

import元素類似于XML模式文檔中的import元素,用于從其他WSDL文檔中導入WSDL定義;

在本例中Types中就是引用的外部的schema檔案,在浏覽器中輸入http://localhost:6789/ns?xsd=1,即可以看到定義的資料類型和元素。

  • message元素使用在WSDL文檔的type元素中定義或在import元素引用的外部WSDL文檔中定義的XML模式的内置類型、複雜類型。
  • portType元素和operation元素描述了Web服務的接口并定義了他的方法,portType元素和operation元素類似于java接口和接口中定義的方法聲明,operation元素使用一個或者多個message類型來定義他的輸入和輸出的有效負載;
  • Binding元素将portType元素和operation元素賦給一個特殊的協定和編碼樣式;
  • Port描述的是一個服務通路入口的部署細節,包括通過哪個Web位址(URL)來通路,應當使用怎樣的消息調用模式來通路等。其中消息調用模式則是使用Binding結構來表示。
  • service元素負責将Internet位址賦給一個具體的綁定;

1.definition元素

所有的WSDL文檔的根元素都是definition元素。該元素封裝了整個文檔,同時通過其name提供了一個WSDL文檔。除了一個命名空間外,改元素沒有其他作用。以下是definition元素的結構:

<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.wyj.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.wyj.com/" 
name="MyServiceImplService">
</definitions>
           

2、types元素

 WSDL采用了W3C XML模式内置類型作為其基本類型系統。types元素用作一個容器,用于定義XML模式内置類型中沒有描述的各種資料類型。當聲明消息部分的有效負載時,消息定義使用了在types元素中定義的資料類型和元素。在本文的WSDL文檔中的types定義:(由于在服務引用了外部的schema檔案,需要在浏覽器中輸入: http://localhost:6789/ns?xsd=1就可以看到types完整的定義了)

<xs:schema xmlns:tns="http://service.wyj.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://service.wyj.com/">
<xs:element name="add" type="tns:add"/>
<xs:element name="addResponse" type="tns:addResponse"/>
<xs:element name="sayHi" type="tns:sayHi"/>
<xs:element name="sayHiResponse" type="tns:sayHiResponse"/>
<xs:complexType name="sayHi">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHiResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="add">
<xs:sequence>
<xs:element name="arg0" type="xs:int"/>
<xs:element name="arg1" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="return" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
           

上面就是資料類型的定義,其中

sayHi:定義了一個複雜類型,包含了一個string類型的資料,将來用來描述操作參數傳入的部分。

sayHiResponse:定義了一個複雜類型,包含了一個string類型的資料,将來用來描述操作的傳回值。

add:定義了一個複雜類型,包含了2個int類型的資料,将來用來描述操作的參數的傳入部分。

addResponse:定義了一個負載類型,包含了一個string類型的資料,将來用作操作的傳回值。

3、import元素

import元素使得可以在目前的WSDL文檔中使用其他WSDL文檔中指定的命名空間中的定義元素。如:

<types>
<xsd:schema>
<xsd:import namespace="http://service.wyj.com/" schemaLocation="http://localhost:6789/ns?xsd=1"/>
</xsd:schema>
</types>
           

必須有namespace屬性和location屬性:

namespace屬性:值必須與正導入的WSDL文檔中聲明的targetNamespace相比對;

location屬性:必須指向一個實際的WSDL文檔,并且該文檔不能為空。

4、message元素

message元素描述了Web服務使用消息的有效負載。message元素可以描述輸出或者接受消息的有效負載;還可以描述SOAP檔案頭和錯誤detail元素的内容。如:

<message name="add">
<part name="parameters" element="tns:add"/>
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse"/>
</message>
<message name="sayHi">
<part name="parameters" element="tns:sayHi"/>
</message>
<message name="sayHiResponse">
<part name="parameters" element="tns:sayHiResponse"/>
</message>
           

該部分是消息格式的抽象定義:定義了四個消息sayHi和sayHiResponse,以及add和addResponse。(以下隻對add和addResponse進行說明,其他2個讀者可以自行類推)

add:由一個消息片段組成,該消息片段名稱是parameters,包含的具體元素類型是add。(前面已經定義過了)

addResponse:由一個消息片段組成,該消息片段名稱是parameters,包含的具體元素是addResponse。(前面已經定義過了)

5.portType元素

portType元素定義了Web服務的抽象接口。該接口有點類似Java的接口,都是定義了一個抽象類型和方法,沒有定義實作。在WSDL中,portType元素是由binding和service元素來實作的,這兩個元素用來說明Web服務實作使用的Internet協定、編碼方案以及Internet位址。

一個portType中可以定義多個operation,一個operation可以看作是一個方法,本文中WSDL文檔的定義:

<portType name="IMyservice">
<operation name="add">
<input wsam:Action="http://service.wyj.com/IMyservice/addRequest" message="tns:add"/>
<output wsam:Action="http://service.wyj.com/IMyservice/addResponse" message="tns:addResponse"/>
</operation>
<operation name="sayHi">
<input wsam:Action="http://service.wyj.com/IMyservice/sayHiRequest" message="tns:sayHi"/>
<output wsam:Action="http://service.wyj.com/IMyservice/sayHiResponse" message="tns:sayHiResponse"/>
</operation>
</portType>
           

portType定義了服務的調用模式的類型,這裡包含了add操作和sayHi操作,同時包含input和output表明該操作是一個響應操作,其中add操作的請求消息是add,響應消息是addResponse;sayHi操作的請求消息是sayHi,響應消息是sayHiResponse。

6、binding

binding元素将一個抽象portType映射到一組具體協定(SOAP和HTTP)、消息傳遞樣式、編碼樣式。通常binding元素與協定專有的元素和在一起使用,如:

<binding name="MyServiceImplPortBinding" type="tns:IMyservice">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="add">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="sayHi">...</operation>
</binding>
           

這部分将服務通路點的抽象定義與SOAP HTTP綁定,描述如何通過SOAP/HTTP來通路按照前面描述的通路入口點類型部署的通路入口。其中規定了在具體SOAP調用時,應當使用的soapAction是""。

具體的使用需要參考特定協定定義的元素。

7.service元素和port元素

service元素包含一個或者多個port元素,其中每個port元素表示一個不同的Web服務。port元素将URL賦給一個特定的binding,甚至可以使兩個或者多個port元素将不同的URL指派給相同的binding。文檔中的例子:

<service name="MyServiceImplService">
<port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
<soap:address location="http://localhost:6789/ns"/>
</port>
</service>
           

這部分是具體的Web服務的定義,在這個名為MyServiceImplService的Web服務中,提供了一個服務通路入口,通路位址是"http://localhost:6789/ns,使用的消息模式是由前面的binding所定義的。

以上就是對WSDL文檔的一個比較詳細分析,希望對大家學習webservice能夠有所幫助。

參考資料:

1.http://www.ibm.com/developerworks/cn/webservices/ws-wsdl/index.html

2.http://kalogen.iteye.com/blog/418958