wsdl文件结构分析——转载

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的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

 

转载出处:http://jameswxx.iteye.com/blog/250759

posted @ 2012-04-28 15:40  SunRain117  阅读(472)  评论(0编辑  收藏  举报