gSoap開發(三)——WSDL簡介

gSoap開發(三)——WSDL簡介

1、WSDL簡介

1WSDL簡介

        WSDL(網絡服務描述語言,Web Services Description Language)是一門基於XML的語言,用於描述WebService以及如何對它們進行訪問。編程

    WSDLWeb服務描述定義爲一組服務訪問點,客戶端能夠經過這些服務訪問點對包含面向文檔信息或面向過程調用的服務進行訪問。WSDL首先對訪問的操做和訪問時使用的請求/響應消息進行抽象描述,而後將其綁定到具體的傳輸協議和消息格式上以最終定義具體部署的服務訪問點。網絡

二、WSDL文檔

        WSDL是一個用於精確描述Web服務的文檔,WSDL文檔是一個遵循WSDL XML模式的XML文檔。WSDL文檔將Web服務定義爲服務訪問點或端口的集合。在WSDL中,因爲服務訪問點和消息的抽象定義已從具體的服務部署或數據格式綁定中分離出來,所以能夠對抽象定義進行再次使用:消息,指對交換數據的抽象描述;而端口類型,指操做的抽象集合。用於特定端口類型的具體協議和數據格式規範構成了能夠再次使用的綁定。將Web訪問地址與可再次使用的綁定相關聯,能夠定義一個端口,而端口的集合則定義爲服務。數據結構

        WSDL文檔一般包含7個重要的元素,即typesimportmessageportTypeoperationbindingservice元素。全部元素嵌套在definitions元素中,definitionsWSDL文檔的根元素。ide

    Types - 數據類型定義的容器,使用某種類型系統(通常地使用XML Schema中的類型系統)模塊化

    Message - 通訊消息的數據結構的抽象類型化定義。使用Types所定義的類型來定義整個消息的數據結構。 函數

    Operation - 對服務中所支持的操做的抽象描述,通常單個Operation描述了一個訪問入口的請求/響應消息對。 編碼

    PortType - 對於某個訪問入口點類型所支持的操做的抽象集合,這些操做能夠由一個或多個服務訪問點來支持。 spa

    Binding - 特定端口類型的具體協議和數據格式規範的綁定。 orm

    Port - 定義爲協議/數據格式綁定與具體Web訪問地址組合的單個服務訪問點。 xml

    Service- 相關服務訪問點的集合。

WSDL文檔主要結構以下:

<definitions>
<types>   definition of types........</types>
<message>   definition of a message....</message>
<portType>   definition of a port.......</portType>
<binding>   definition of a binding....</binding>
</definitions>

wKiom1joR9eTMaDbAACPLvMAM_4771.png

WSDL文檔實例:

<message name="getTermRequest">
   <part name="term" type="xs:string"/>
</message>
 
<message name="getTermResponse">
   <part name="value" type="xs:string"/>
</message>
 
<portType name="glossaryTerms">
  <operation name="getTerm">
        <input message="getTermRequest"/>
        <output message="getTermResponse"/>
  </operation>
</portType>


        <portType>元素把"glossaryTerms"定義爲某個端口的名稱,把 "getTerm"定義爲某個操做的名稱。

    操做"getTerm"擁有一個名爲"getTermRequest"的輸入消息,以及一個名爲"getTermResponse"的輸出消息。

        <message>元素可定義每一個消息的部件,以及相關聯的數據類型。

    對比傳統的編程,glossaryTerms是一個函數庫,而"getTerm"是帶有輸入參數"getTermRequest"和返回參數getTermResponse的一個函數。

3WSDL文檔閱讀

    WSDL文檔的閱讀順序從下往上讀。

    每一個WSDL有且只有一個Service節點。

    A、先找Service節點

    BService節點中找port節點。每一個port對應一個PortType

    CPort節點對應一binding節點。每一個binding節點對應一個PortType

    DPortType中有operation 節點就是服務的方法。

    Eoperation 中有Input(參數)和output(返回值)

    FInput(參數)和output(返回值)對應message節點

    GMessage對應element節點。Element節點對應complexType節點描述了參數及返回值的數據類型。

2、WSDL文檔解析

簡單的Web ServiceWSDL文檔以下:

<?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/services/HelloService" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>


    服務支持名爲sayHello的惟一操做,sayHello操做經過在http上運行SOAP協議來實現的。請求接受一個字符串name,通過處理後返回一個簡單的字符串。

1definitions元素

    全部的WSDL文檔的根元素均是definitions元素。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>


2types元素

         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:定義了一個複雜類型,僅僅包含一個簡單的字符串,未來用來描述操做的返回值;

3import元素

        import元素使得能夠在當前的WSDL文檔中使用其餘WSDL文檔中指定的命名空間中的定義元素。本例中沒有使用import元素。一般在用戶但願模塊化WSDL文檔的時候,import功能是很是有效果的。

        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文檔,而且該文檔不能爲空。

4message元素

        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>


    消息格式的抽象定義:定義了兩個消息sayHelloResponsesayHelloRequest

        sayHelloRequestsayHello操做的請求消息格式,由一個消息片段組成,名字爲parameters,元素是咱們前面定義的types中的元素;

        sayHelloResponsesayHello操做的響應消息格式,由一個消息片段組成,名字爲parameters,元素是咱們前面定義的types中的元素;

     若是採用RPC樣式的消息傳遞,只須要將文檔中的element元素應以修改成type便可。

5portType元素

        portType元素定義了Web服務的抽象接口。該接口有點相似Java的接口,都是定義了一個抽象類型和方法,沒有定義實現。在WSDL中,portType元素是由bindingservice元素來實現的,這兩個元素用來講明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方法,同時包含inputoutput代表該操做是一個請求/響應模式,請求消息是前面定義的sayHelloRequest,響應消息是前面定義的sayHelloResponseinput表示傳遞到Web服務的有效負載,output消息表示傳遞給客戶的有效負載。

6binding

        binding元素將一個抽象portType映射到一組具體協議(SOAOHTTP)、消息傳遞樣式、編碼樣式。一般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>


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

7service元素和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/services/HelloService" />
        </wsdl:port>
</wsdl:service>


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

相關文章
相關標籤/搜索