WebService 之 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>


一、WSDL概述
        WebServices Description Language (WSDL Web服务语言)是一个用于精确描述Web Service的文档格式。
        WSDL非常适合于用作代码生成器,它能够读取WSDL文档,并且可以为访问Web服务生成一个程序化的接口,大多数软件供应商和主要的标准机构(包括 W3C、WS-I和OASIS)都支持WSDL。例如:JAX-RPC provider(例如:BEA Weblogic)通过API用WSDL生成相应的占位程序;IBM WebSphere、Microsoft.NET以及Apache Axis都有自己的工具生成相关的代码。下图是一个例子:
                                       

       上面的例子JAX-RPC通过读取WSDL文档,创建JAX-RPC RMI接口(endpoint接口)和实现此接口的网络占位程序(stub)。客户端程序通过RMI接口,Stub和Web Service服务端交换SAOP消息。

二、WSDL基本结构
        WSDL文档是一个遵循WSDL XML模式的XML文档(文档实例);类似于:SOAP文档是一个遵循SOAP XML模式的XML文档(文档实例);
        一个WSDL文档的根元素是definitions元素,WSDL文档包含7个重要的元素:types, import, message, portType, operations, binding和service元素。

三、WSDL声明
      
        3.1 XML声明

           <?xml version="1.0" encoding="UTF-8"?>

            WSDL的声明必须定义成使用:UTF-8 或者UTF-16 编码。

        3.2 definition元素
              所有WSDL文档的根元素都是definition元素。   

<definitions name="BookQuoteWS"
                      targetNamespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:mh
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:soapbind
="http://schemas.xmlsoap.org/wsdl/soap/"
                         xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
                     xmlns
="http://schemas.xmlsoap.org/wsdl/">

 definition元素中一般包括若干个XML命名空间;
 http://schemas.xmlsoap.org/wsdl/是默认的命名空间,这样就可以不用显式地定义每一个WSDL元素的命名空间了,例如:
<types> <messages> <portType>…;文档中所有的元素缺省应该属于这个命名空间。
definition元素的的一个属性是name,此属性不重要可以没有;
   定义了targetNamespace命名空间,它为在模式中显式创建的所有新类型均声明了XML命名空间,而且上面的例子中赋予了mh前缀。

<!-- message elements describe the input and output parameters -->

<message name="GetBookPriceRequest">

      
<part name="isbn" type="xsd:string" />

</message>

<message name="GetBookPriceResponse">

     
<part name="price" type="xsd:float" />

</message>

<!-- portType element describes the abstract interface of a Web service -->

<portType name="BookQuote">

    
<operation name="getBookPrice">

          
<input name="isbn" message="mh:GetBookPriceRequest"/>

          
<output name="price" message="mh:GetBookPriceResponse"/>

  
</operation>

</portType>

 

         上面的例子中:message元素利用name属性指定了标签(例如:GetBookPriceRequest),这些标签会自动使用targetNamespace的命名空间,标签了的messages元素通常被称为定义。
          文档中的其他元素用标签和命名空间前缀去应用定义,例如上面的例子中:input元素是使用mh:GetBookPriceRequest来引用标签GetBookPriceRequest。

         3.3 Types元素
               Types元素用作一个容器,定义了自定义的特殊数据类型,在声明消息部分(有效负载)的时候,messages定义使用了types元素中定义的数据类型与元素。

<?xml version="1.0" encoding="UTF-8"?>

<definitions name="BookQuoteWS"
                      targetNamespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:mh
="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:soapbind
="http://schemas.xmlsoap.org/wsdl/soap/"
                      xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
                     xmlns
="http://schemas.xmlsoap.org/wsdl/">
<types>

    
<xsd:schema   targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote">

      
<!-- The ISBN simple type -->

      
<xsd:simpleType name="ISBN">

        
<xsd:restriction base="xsd:string">

          
<xsd:pattern value="[0-9]{9}[0-9Xx]" />

        
</xsd:restriction>

      
</xsd:simpleType>

    
</xsd:schema>

</types>

         Types元素作为一个容器,用来定义XML模式内置的数据类型(即复杂类型和定制的简单类现,详细见Web Service XML文章)中没有描述的各种数据类型。例如:ISBN。
        上面的例子中,types元素中直接嵌套了一个完整的W3C XML模式文档,此文档中targetNamespace必须是一个有效的非空值,而且必须属于由WSDL文档。

      3.4 Import元素
            Import元素可以让当前的文档使用其他WSDL文档中指定命名空间中的定义。 

<definitions name="AllMhWebServices"
         xmlns
="http://schemas.xmlsoap.org/wsdl/">

    
<import namespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"

     location
="http://www.Monson-Haefel.com/jwsbook/BookPrice.wsdl"/>

    
<import namespace="http://www.Monson-Haefel.com/jwsbook/po"

     location
="http://www.Monson-Haefel.com/jwsbook/wsdl/PurchaseOrder.wsdl"/>

    
<import namespace="http://www.Monson-Haefel.com/jwsbook/Shipping"

     location
="http://www.Monson-Haefel.com/jwsbook/wsdl/Shipping.wsdl"/>

</definitions >

          WSDL的import元素必须声明两个属性,即namespace属性和location属性。
          namespace属性必须和正导入的WSDL文档中声明的targetNamespace相匹配。
          location属性必须指向一个实际的WSDL文档。

四、WSDL抽象接口
         Message、portType和operation元素用于描述Web服务的抽象接口,相当于JAVA或者C++中编程中的类的接口。其中portType相当于类接口的名称;operation相当于接口中包含的函数,message相当于函数的参数和返回值。
        
        4.1 Message元素
              Message元素描述了Web服务的有效负载。相当于函数调用中的参数和返回值。

<message name="GetBulkBookPriceRequest">

    
<part name="isbn" type="xsd:string"/>

    
<part name="quantity" type="xsd:int"/>

  
</message>

  
<message name="GetBulkBookPriceResponse">

    
<part name="price" type="mh:prices" />

  
</message>

RPC式样的Web服务的message服务

GetBulkBookPriceRequest表示消息的输入(相当于函数的参数),GetBulkBookPriceResponse表示消息的输出(相当于函数的返回值)

Web Service的输入和输出参数可以是多个(一个特点),每一个输入或者输出使用part元素定义,RPC样式必须使用type来定义类型

RPC样式用类型来数据定义过程调用,调用中的每一个元素表示某一个类型的参数。

<types>

    
<xsd:schema targetNamespace="http://www.Monson-Haefel.com/jwsbook/PO">

      
<!-- Import the PurchaseOrder XML schema document -->

      
<xsd:import namespace="http://www.Monson-Haefel.com/jwsbook/PO"

       schemaLocation
="http://www.Monson-Haefel.com/jwsbook/po.xsd" />

    
</xsd:schema>

  
</types>

  
<!-- message elements describe the input and output parameters -->

  
<message name="SubmitPurchaseOrderMessage">

    
<part name="order" element="mh:purchaseOrder" />

  
</message>

文档式样Web服务的Messages元素:

当用户采用文档式样消息传递模式的时候,messages元素要应用types定义中的顶级元素。具体顶级元素的定义和XML schema详见Web Server XML文档。

消息部分使用element属性定义

文档式样的消息传递要交换XML文档,并且应用它们的顶级元素。

注:Messages元素的RPC/Document试样对应了SOAP RPC/Document消息传递模式,详细见Web Server SOAP相关文档

<types>

    
<xsd:schema targetNamespace="http://www.Monson-Haefel.com/jwsbook/PO">

      
<!-- Import the PurchaseOrder XML schema document -->

      
<xsd:element name="InvalidIsbnFaultDetail" >

        
<xsd:complexType>

          
<xsd:sequence>

            
<xsd:element name="offending-value" type="xsd:string"/>

            
<xsd:element name="conformance-rules" type="xsd:string" />

          
</xsd:sequence>

        
</xsd:complexType>

      
</xsd:element>

    
</xsd:schema>

  
</types>

 

  
<!-- message elements describe the input and output parameters -->

  
<message name="GetBookPriceRequest">

    
<part name="isbn" type="xsd:string" />

  
</message>

  
<message name="GetBookPriceResponse">

    
<part name="price" type="xsd:float" />

  
</message>

  
<message name="InvalidArgumentFault">

    
<part name="error_message" element="mh:InvalidIsbnFaultDetail" />

  
</message>

 

声明错误消息:

错误使用的消息定义只能采用Document/Literal编码样式

上面声明了匿名类型,InvalidIsbnFaultDetail不需要type类型,complexType中也不包括name属性,详细见Web Service XML相关文档。


       4.2 portType元素
             PortType元素定义了Web服务的抽象接口,它可以由一个或者多个operation元素,每个operation元素定义了一个RPC样式或者文档样式的Web服务方法。

       4.3 operation元素
            Operation元素要用一个或者多个messages消息来定义它的输入、输出以及错误。

<message name="GetBulkBookPriceRequest">

  
<part name="isbn" type="xsd:string"/>

  
<part name="quantity" type="xsd:int"/>

</message>

<message name="GetBulkBookPriceResponse">

  
<part name="prices" type="mh:prices" />

</message>

<message name="InvalidArgumentFault">

    
<part name="error_message" element="mh:InvalidIsbnFaultDetail" />

  
</message>

<portType name="GetBulkBookPrice" >

  
<operation name="getBulkBookPrice" parameterOrder="isbn quantity">

     
<input name="request" message="mh:GetBulkBookPriceRequest"/>

     
<output name="prices" message="mh:GetBulkBookPriceResponse"/>

<fault name="InvalidArgumentFault" message="mh:InvalidArgumentFault"/>

  
</operation>

</portType>

Input表示传递到Web服务的有效负载;output表示返回给客户的有效负载;可以不包括,也可以包括一个或者多个fault错误消息。

parameterOrder定义了input和output消息采用的正确的顺序

使用parameterOrder的时候,必须包含所有输入参数部分;并且只包含不是返回类型的输出部分,如果output只有一个part(上例),会假设返回值,所以不包括在parameterOrder中

如果parameterOrder列出output中的part部分,那么这个将被作为OUT参数,如果input元素和output元素使用相同的名称声明了一个部分的时候,此部分为INOUT参数

        4.4 WSDL消息交换模式(MEP)
              Messaging Exchange Patterns(MEP)
              Web服务中使用了四种消息交换模式,即请求/响应、单向、通知以及恳求/响应模式。大多数基于WSDL的web服务使用请求/响应和单向两种模式。
              WSDL通过operation元素的input/output来定义使用那种模式,如果有input+output+可选的fault参数,那就使用请求/响应模式;如果只使用input,那就使用单向模式。
              在通知模式中:Web服务将消息发送给客户,但不等待回复;一般客户通过注册来接收通知;在恳求/响应模式中类似通知模式,唯一的区别要期待客户对Web服务的响应。

五、WSDL实现:binding元素
        Binding元素将一个抽象的portType映射到一组具体的协议(SOAP或者HTTP)、消息传递样式(RPC或者document)以及编码样式(literal或者SOAP encoding)。
        Binding的类似于将接口或者函数的调用绑定到某种协议上:例如CORBA、COM或者RPC的方式,这里使用SOAP协议。

        5.1 soapbind:binding元素

<binding name="BookPrice_Binding" type="mh:BookQuote">

  
<soapbind:binding style="rpc"

   transport
="http://schemas.xmlsoap.org/soap/http"/>

  
<operation name="getBookPrice">

soapbind:binding元素指定了用于传输SOAP消息的Internet协议以及operation缺省的消息类型(RPC还是文档类型)

http://schemas.xmlsoap.org/soap/http表示采用的是HTTP的传输方式,当然也可以用HTTPS,用户具体使用HTTP还是HTTPS取决于Port元素中定义的location属性声明中的模式。

上面的rpc表示缺省状态下:operation将采用RPC的方式传递消息负载。

         5.2 soapbind:operation元素
<operation name="getBookPrice">

    
<soapbind:operation style="rpc"

     soapAction
=

     "http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice"
/>

POST 1ed/BookQuote HTTP/1.1

Host: www.Monson-Haefel.com

Content-Type: text/xml; charset="utf-8"

Content-Length: nnnn

SOAPAction="http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice"

soapbind:operation元素指定了消息传递样式(RPC或者document),并且指定了SOAPAction字段的值。

上面的例子显示在HTTP消息中的SOAPAction中对应的值

       5.3 soapbind:body元素
<operation name="getBookPrice">

<soapbind:operation style="rpc"/>

<input>

          
<soapbind:body use="literal"

          namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

       
</input>

       
<output>

          
<soapbind:body use="literal"

          namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

       
</output>

<operation name="submit">

  
<soapbind:operation style="document"/>

      
<input>

        
<soapbind:body use="literal" />

      
</input>

      
<output>

        
<soapbind:body use="literal" />

      
</output>

</operation>

soapbind:body元素有四个属性use、namespace、part和encodingStyle

对于WS-I use的属性值必须是literal,意味这不是用编码的方式,所以永远不会用到encodingStyle属性

在RPC样式中,必须用一个有效的URI指定的namespace属性。此URI可以于WSDL文档的targetNampspce相同;而在document样式中不能使用namespace,XML文档样式的命名空间派生于它的XML文档

          5.4 soapbind:fault元素
<fault name="InvalidArgumentFault">

     
<soapbind:fault name="InvalidArgumentFault" use="literal" />

</fault>

<portType name="BookQuote">

  
<operation name="getBookPrice">

     
<input name="isbn" message="mh:GetBookPriceRequest"/>

     
<output name="price" message="mh:GetBookPriceResponse"/>

     
<fault name="InvalidArgumentFault" message="mh:InvalidArgumentFault"/>

  
</operation>

</portType>

soapbind:fault元素和fault元素包含一个强制性的name属性,表示要引用声明于对应portType中的专有错误消息

          5.5 soapbind:header元素
<types>

<xsd:schema 

targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"

     xmlns
="http://www.w3.org/2001/XMLSchema">

        
<xsd:element name="message-id" type="string" />

    
</xsd:schema>

</types>

 

<!-- message elements describe the input and output parameters -->

  
<message name="Headers">

    
<part name="message-id" element="mh:message-id" />

  
</message>

 

<operation name="getBookPrice">

  
<input>

     
<soapbind:header message="mh:Headers" part="message-id" use="literal" />

     
<soapbind:body use="literal"

           namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

        
</input>

WSDL在绑定的input元素、output元素中利用soapbind:header元素显式指定了一个SOAP头文件

        5.6 soapbind:headerfault元素  
<!-- message elements describe the input and output parameters -->

  
<message name="HeaderFault">

    
<part name="faultDetail" element="mh:detailMessage" />

  
</message>

 

<input>

      
<soapbind:header message="mh:Header" use="literal">

         
<soapbind:headerfault message="mh:Headers" use="literal" />

      
</soapbind:header>

      
<soapbind:body use="literal"

           namespace
="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

   
</input>

soapbind:headerfault元素表述了Header专用的错误消息,如果有一个响应消息,必须在消息的Header元素中返回各种header的专用错误。

SOAP没有就如何提供Header错误方面给出详细说明,只是要求必须在Header元素中包含detail元素。有些SOAP工具箱将SOAP的fault放在header元素中。

六、WSDL实现:Service和Port元素
<service name="BookPriceService">

  
<port name="BookPrice_Port" binding="mh:BookPrice_Binding">

    
<soapbind:address location=

     "http://www.Monson-Haefel.com/jwsbook/BookQuote"
 />

  
</port>

  
<port name="BookPrice_Failover_Port" binding="mh:BookPrice_Binding">

    
<soapbind:address location=

     "http://www.monson-haefel.org/jwsbook/BookPrice"
 />

  
</port>

  
<port name="SubmitPurchaseOrder_Port"

   binding
="mh:SubmitPurchaseOrder_Binding">

    
<soapbind:address location=

     "https://www.monson-haefel.org/jwsbook/po"
 />

  
</port>

</service>

Service元素包含一个或者多个Port元素

每一个Port元素对应一个不同的Web服务,port将一个URL赋予一个特定的binding,通过location实现

可以使两个或者多个port元素将不同的URL赋给相同的binding,例如负载平衡和容错的时候,使用这种方法。

soapbind:address:将Internet地址通过location属性赋予一个SOAP绑定。


转载地址:http://blog.csdn.net/tropica/article/details/3203892
http://blog.csdn.net/liguocai2005/article/details/4402350
发布了50 篇原创文章 · 获赞 5 · 访问量 11万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章