wsdl檔案結構分析

悠悠隱於市發表於2011-06-16

wsdl檔案結構分析

原文來自:http://webservices.group.iteye.com/group/topic/11467

 


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地址賦給一個具體的繫結;

1、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>

2、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:定義了一個複雜型別,僅僅包含一個簡單的字串,將來用來描述操作的返回值;

3、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文件,並且該文件不能為空。

4、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即可。

5、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訊息表示傳遞給客戶的有效負載。

相關文章