WSDL文件詳解

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的文檔結構圖:WSDL文檔元素的結構圖

WSDL的xml schema可以參照如下網址:http://schemas.xmlsoap.org/wsdl/

三:WSDL的基本結構--詳述

本節將通過一個例子詳細描述WSDL文檔每個元素的作用。下面一個例子是一個簡單的WSDL文檔的內容,該文檔的產生可以參見我的另外一篇文章:xfire開發實例--HelloWorld篇 (http://blog.csdn.net/juxtapose/archive/2007/09/10/1779849.aspx)。

一個簡單的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消息表示傳遞給客戶的有效負載。

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>
        
</wsdl:operation>
    
</wsdl:binding>

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

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

7、service元素和port元素

service元素包含一個或者多個port元素,其中每個port元素表示一個不同的Web服務。port元素將URL賦給一個特定的binding,甚至可以使兩個或者多個port元素將不同的URL賦值給相同的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>

這部分是具體的Web服務的定義,在這個名爲HelloService的Web服務中,提供了一個服務訪問入口,訪問地址是http://localhost:8080/xfire/services/HelloService,使用的消息模式是由前面的binding所定義的。

  本文簡單介紹了WSDL規範的用途,基本結構和使用方法,希望對大家學習WSDL有幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章