`

WSDL 详解

 
阅读更多

WSDL 详解

 

WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,

他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务访问点

对包含面向文档信息或面向过程调用的服务进行访问(类似远程过程调用)。

WSDL首先对访问的操作和访问时使用的请求/响应消息进行抽象描述,

然后将其绑定到具体的传输协议和消息格式上以最终定义具体部署的服务访问点。

相关的具体部署的服务访问点通过组合就成为抽象的Web服务。 

本文将详细讲解WSDL文档的结构,并分析每个元素的作用。

 

wsdl文件结构参考:



 

相应的提供服务的Java类:

package test.sevices;

public class TestService
{
  private String name ="Jack";
  
  public void setName(String newName)
  {
    this.name = newName;
  }
  
  public String getName()
  {
    return name;
  }
}

 

一: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文档的内容,

 

♦ 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地址赋给一个具体的绑定;

 

用到的wsdl文件:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions 
			  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
			  xmlns:ns1="http://org.apache.axis2/xsd" 
			  xmlns:ns="http://quickstart.samples/xsd" 
			  xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 
			  xmlns:tns="http://quickstart.samples/" 
			  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
			  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
			  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
			  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
			  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
			  targetNamespace="http://quickstart.samples/">
    
    <wsdl:documentation>TestService</wsdl:documentation>
    
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd">
            <xs:element name="setName">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="newName" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getName">
                <xs:complexType>
                    <xs:sequence/>
                </xs:complexType>
            </xs:element>
            <xs:element name="getNameResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    
    <wsdl:message name="getNameRequest">
        <wsdl:part name="parameters" element="ns:getName"/>
    </wsdl:message>
    <wsdl:message name="getNameResponse">
        <wsdl:part name="parameters" element="ns:getNameResponse"/>
    </wsdl:message>
    <wsdl:message name="setNameRequest">
        <wsdl:part name="parameters" element="ns:setName"/>
    </wsdl:message>
    
    <wsdl:portType name="TestServicePortType">
        <wsdl:operation name="getName">
            <wsdl:input message="tns:getNameRequest" wsaw:Action="urn:getName"/>
            <wsdl:output message="tns:getNameResponse" wsaw:Action="urn:getNameResponse"/>
        </wsdl:operation>
        <wsdl:operation name="setName">
            <wsdl:input message="tns:setNameRequest" wsaw:Action="urn:setName"/>
        </wsdl:operation>
    </wsdl:portType>
    
    <wsdl:binding name="TestServiceSoap11Binding" type="tns:TestServicePortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getName">
            <soap:operation soapAction="urn:getName" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="setName">
            <soap:operation soapAction="urn:setName" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
        </wsdl:operation>
    </wsdl:binding>
    
    <wsdl:binding name="TestServiceSoap12Binding" type="tns:TestServicePortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getName">
            <soap12:operation soapAction="urn:getName" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="setName">
            <soap12:operation soapAction="urn:setName" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
        </wsdl:operation>
    </wsdl:binding>
    
    <wsdl:binding name="TestServiceHttpBinding" type="tns:TestServicePortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="getName">
            <http:operation location="getName"/>
            <wsdl:input>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="setName">
            <http:operation location="setName"/>
            <wsdl:input>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:input>
        </wsdl:operation>
    </wsdl:binding>
    
    <wsdl:service name="TestService">
        <wsdl:port name="TestServiceHttpSoap11Endpoint" binding="tns:TestServiceSoap11Binding">
            <soap:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpSoap11Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="TestServiceHttpSoap12Endpoint" binding="tns:TestServiceSoap12Binding">
            <soap12:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpSoap12Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="TestServiceHttpEndpoint" binding="tns:TestServiceHttpBinding">
            <http:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpEndpoint/"/>
        </wsdl:port>
    </wsdl:service>
    
</wsdl:definitions>

 

 

1、definitions元素

 

所有的WSDL文档的根元素均是definitions元素。该元素封装了整个文档,同时通过其name提供了一个WSDL文档。除了提供一个命名空间外,该元素没有其他作用,故不作详细描述。

 

下面的代码是一个definitions元素的结构:

 

<wsdl:definitions 

  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 

  xmlns:ns1="http://org.apache.axis2/xsd" 

  xmlns:ns="http://quickstart.samples/xsd" 

  xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 

  xmlns:tns="http://quickstart.samples/" 

  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 

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

  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 

  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 

  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 

  targetNamespace="http://quickstart.samples/">

</wsdl:definitions>

 

2、types元素

 

 WSDL采用了W3C XML模式内置类型作为其基本类型系统。

 types元素用作一个容器,用于定义XML模式内置类型中没有描述的各种数据类型。

 当声明消息部分的有效负载时,消息定义使用了在types元素中定义的数据类型和元素。

 

 <wsdl:types>

        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://quickstart.samples/xsd">

            <xs:element name="setName">

                <xs:complexType>

                    <xs:sequence>

                        <xs:element minOccurs="0" name="newName" nillable="true" type="xs:string"/>

                    </xs:sequence>

                </xs:complexType>

            </xs:element>

            

            <xs:element name="getName">

                <xs:complexType>

                    <xs:sequence/>

                </xs:complexType>

            </xs:element>

            

            <xs:element name="getNameResponse">

                <xs:complexType>

                    <xs:sequence>

                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>

                    </xs:sequence>

                </xs:complexType>

            </xs:element>

            

        </xs:schema>

    </wsdl:types>

 

因为提供服务的Java类有两个方法 void setName(String newName)和String getName()

这样,上面定义的types定义与上面的方法对应,

有返回值的方法定义的type名字为: 方法名+NameResponse.

方法名字是一个最外层元素,方法的参数为内曾经元素.

返回值类型最为一个内层参数,参数名称为name="return"

 

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、portType和operation元素用于描述Web服务的抽象接口,相当于JAVA或者C++中编程中的类的接口。

其中portType相当于类接口的名称;

operation相当于接口中包含的函数,

message相当于函数的参数和返回值。

 

message元素描述了Web服务使用消息的有效负载。

message元素可以描述输出或者接受消息的有效负载;

还可以描述SOAP文件头和错误detail元素的内容。

定义message元素的方式取决于使用RPC样式还是文档样式的消息传递。

 

Message元素描述了Web服务的有效负载。相当于函数调用中的参数和返回值。

 

在本文中的message元素的定义,本文档使用了采用文档样式的消息传递:

 

    <wsdl:message name="getNameRequest">

        <wsdl:part name="parameters" element="ns:getName"/>

    </wsdl:message>

    <wsdl:message name="getNameResponse">

        <wsdl:part name="parameters" element="ns:getNameResponse"/>

    </wsdl:message>

    <wsdl:message name="setNameRequest">

        <wsdl:part name="parameters" element="ns:setName"/>

    </wsdl:message>

    

如果采用RPC样式的消息传递,只需要将文档中的element元素应以修改为type即可。

 

5、portType元素

 

portType元素定义了Web服务的抽象接口。

该接口有点类似Java的接口,都是定义了一个抽象类型和方法,没有定义实现。

在WSDL中,portType元素是由binding和service元素来实现的,

这两个元素用来说明Web服务实现使用的Internet协议、编码方案以及Internet地址。

 

一个portType中可以定义多个operation,一个operation可以看作是一个方法,

本文中WSDL文档的定义:

 

    <wsdl:portType name="TestServicePortType">

        <wsdl:operation name="getName">

            <wsdl:input message="tns:getNameRequest" wsaw:Action="urn:getName"/>

            <wsdl:output message="tns:getNameResponse" wsaw:Action="urn:getNameResponse"/>

        </wsdl:operation>

        <wsdl:operation name="setName">

            <wsdl:input message="tns:setNameRequest" wsaw:Action="urn:setName"/>

        </wsdl:operation>

    </wsdl:portType>

 

portType定义了服务的调用模式的类型,

这里包含一个操作getName方法,同时包含input和output表明该操作是一个请求/响应模式,

请求消息是前面定义的getNameRequest,

响应消息是前面定义的getNameResponse。

input表示传递到Web服务的有效负载,

output消息表示传递给客户的有效负载。

 

WSDL消息交换模式(MEP)

  Messaging Exchange Patterns(MEP)

  Web服务中使用了四种消息交换模式,即请求/响应、单向、通知以及恳求/响应模式。

  大多数基于WSDL的web服务使用请求/响应和单向两种模式。

  WSDL通过operation元素的input/output来定义使用那种模式,如果有input+output+可选的fault参数,

  那就使用请求/响应模式;如果只使用input,那就使用单向模式。

  在通知模式中:Web服务将消息发送给客户,但不等待回复;一般客户通过注册来接收通知;

  在恳求/响应模式中类似通知模式,唯一的区别要期待客户对Web服务的响应。

 

6、binding

 

binding元素将一个抽象portType映射到一组具体协议(SOAO和HTTP)、消息传递样式、编码样式。

通常binding元素与协议专有的元素和在一起使用,本文中的例子:

 

    <wsdl:binding name="TestServiceSoap11Binding" type="tns:TestServicePortType">

        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>

        <wsdl:operation name="getName">

            <soap:operation soapAction="urn:getName" style="document"/>

            <wsdl:input>

                <soap:body use="literal"/>

            </wsdl:input>

            <wsdl:output>

                <soap:body use="literal"/>

            </wsdl:output>

        </wsdl:operation>

        <wsdl:operation name="setName">

            <soap:operation soapAction="urn:setName" style="document"/>

            <wsdl:input>

                <soap:body use="literal"/>

            </wsdl:input>

        </wsdl:operation>

    </wsdl:binding>

    

    <wsdl:binding name="TestServiceSoap12Binding" type="tns:TestServicePortType">

        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>

        <wsdl:operation name="getName">

            <soap12:operation soapAction="urn:getName" style="document"/>

            <wsdl:input>

                <soap12:body use="literal"/>

            </wsdl:input>

            <wsdl:output>

                <soap12:body use="literal"/>

            </wsdl:output>

        </wsdl:operation>

        <wsdl:operation name="setName">

            <soap12:operation soapAction="urn:setName" style="document"/>

            <wsdl:input>

                <soap12:body use="literal"/>

            </wsdl:input>

        </wsdl:operation>

    </wsdl:binding>

    

    <wsdl:binding name="TestServiceHttpBinding" type="tns:TestServicePortType">

        <http:binding verb="POST"/>

        <wsdl:operation name="getName">

            <http:operation location="getName"/>

            <wsdl:input>

                <mime:content type="application/xml" part="parameters"/>

            </wsdl:input>

            <wsdl:output>

                <mime:content type="application/xml" part="parameters"/>

            </wsdl:output>

        </wsdl:operation>

        <wsdl:operation name="setName">

            <http:operation location="setName"/>

            <wsdl:input>

                <mime:content type="application/xml" part="parameters"/>

            </wsdl:input>

        </wsdl:operation>

    </wsdl:binding>

    

    

    这部分将服务访问点的抽象定义与SOAP/HTTP绑定,描述如何通过SOAP/HTTP来访问按照前面描述的访问入口点类型部署的访问入口。

    其中规定了在具体SOAP调用时,应当使用的soapAction是""。

    

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

 

     http://schemas.xmlsoap.org/soap/http表示采用的是HTTP的传输方式,当然也可以用HTTPS,

     用户具体使用HTTP还是HTTPS取决于Port元素中定义的location属性声明中的模式。

     

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

     

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

 

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

 

     在RPC样式中,必须用一个有效的URI指定的namespace属性。此URI可以于WSDL文档的targetNampspce相同;

     而在document样式中不能使用namespace,XML文档样式的命名空间派生于它的XML文档

     

 

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

 

具体的使用需要参考特定协议定义的元素。

 

7、service元素和port元素

 

service元素包含一个或者多个port元素,其中每个port元素表示一个不同的Web服务。

port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的binding。

文档中的例子:

 

    <wsdl:service name="TestService">

        <wsdl:port name="TestServiceHttpSoap11Endpoint" binding="tns:TestServiceSoap11Binding">

            <soap:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpSoap11Endpoint/"/>

        </wsdl:port>

        <wsdl:port name="TestServiceHttpSoap12Endpoint" binding="tns:TestServiceSoap12Binding">

            <soap12:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpSoap12Endpoint/"/>

        </wsdl:port>

        <wsdl:port name="TestServiceHttpEndpoint" binding="tns:TestServiceHttpBinding">

            <http:address location="http://localhost:8080/TestWebService/services/TestService.TestServiceHttpEndpoint/"/>

        </wsdl:port>

    </wsdl:service>

    

 

  • 大小: 106.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics