欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

WSDL元素详解 WSDL简介 有大用

WSDL代表Web服务描述语言。它是描述Web服务的标准格式。WSDL由Microsoft和IBM共同开发。

WSDL的特点

  • WSDL是一种基于XML的协议,用于分散和分布式环境中的信息交换。

     

  • WSDL定义描述了如何访问Web服务及其将执行的操作。

  • WSDL是描述如何与基于XML的服务进行接口的语言。

  • WSDL是通用描述,发现和集成(UDDI)(一个基于XML的全球业务注册表)的组成部分。

  • WSDL是UDDI使用的语言。

  • WSDL发音为'wiz-dull',并被拼写为'WSD-L'。

WSDL使用

WSDL通常与SOAP和XML Schema结合使用,以通过Internet提供Web服务。连接到Web服务的客户端程序可以读取WSDL以确定服务器上可用的功能。使用的任何特殊数据类型都以XML Schema的形式嵌入到WSDL文件中。客户端然后可以使用SOAP来实际调用WSDL中列出的一个函数。

WSDL的历史

2001年3月,由Ariba,IBM和Microsoft提交的WSDL 1.1作为W3C XML活动XML协议的XML服务提交。

WSDL 1.1尚未被万维网联盟(W3C)认可,但它刚刚发布了2.0版的草案,这将是一项建议(官方标准),并得到W3C的认可。

 

WSDL元素

WSDL将Web服务分解成三个特定的,可识别的元素,一旦定义就可以组合或重用。

可以分别定义的WSDL的三个主要元素有:

  • 类型(Types)

  • 操作(Operations)

  • 捆绑(Binding)

WSDL文档具有各种元素,但它们包含在这三个主要元素中,可以作为单独的文档开发,然后可以组合或重用以形成完整的WSDL文件。

WSDL元素

WSDL文档包含以下元素:

  • 定义(Definition:它是所有WSDL文档的根元素。它定义了Web服务的名称,声明在整个文档的其余部分使用的多个命名空间,并包含此处描述的所有服务元素。

  • 数据类型(Data types:消息中使用的数据类型采用XML模式的形式。

  • 消息(Message:它是数据的抽象定义,以消息的形式呈现为整个文档或作为要映射到方法调用的参数。

  • 操作(Operation:消息的操作的抽象定义,例如命名方法,消息队列或业务流程,将接受和处理消息。

  • 端口类型(Port type:映射到一个或多个端点的一组抽象操作,定义绑定操作的集合; 通过各种绑定,可以将抽象的操作集合映射到多个传输。

  • 绑定(Binding:是针对特定端口类型定义的操作和消息的具体协议和数据格式。

  • 端口(Port:绑定和网络地址的组合,提供服务通信的目标地址。

  • 服务(Service:它是包含文件中服务定义的相关端点的集合; 服务将绑定映射到端口并包括任何可扩展性定义。

除了这些主要元素之外,WSDL规范还定义了以下实用程序元素:

  • 文档:此元素用于提供可读取的文档,并可以包含在任何其他WSDL元素中。

  • 导入:此元素用于导入其他WSDL文档或XML模式。

注意:通常使用Web服务感知工具自动生成WSDL部件。

WSDL文档结构

WSDL文档的主要结构如下所示:

<definitions>
   <types>
      definition of types........
   </types>

   <message>
      definition of a message....
   </message>

   <portType>
      <operation>
         definition of a operation.......  
      </operation>
   </portType>

   <binding>
      definition of a binding....
   </binding>

   <service>
      definition of a service....
   </service>
</definitions>

WSDL文档还可以包含其他元素,如扩展元素和服务元素,可以将单个WSDL文档中的多个Web服务的定义分组在一起。

进一步分析WSDL文档的一个例子。

WSDL 例子

下面给出了一个WSDL文件,用于演示一个简单的WSDL程序。

让我们假设该服务提供了一个单独的公开的功能,称为sayHello。此函数期望单个字符串参数并返回单个字符串问候语。例如,如果传递参数世界,那么服务函数sayHello会返回问候语“Hello,world!”。

HelloService.wsdl文件的内容:

<definitions name="HelloService"
   targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl"
   xmlns="http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
   <message name="SayHelloRequest">
      <part name="firstName" type="xsd:string"/>
   </message>
	
   <message name="SayHelloResponse">
      <part name="greeting" type="xsd:string"/>
   </message>

   <portType name="Hello_PortType">
      <operation name="sayHello">
         <input message="tns:SayHelloRequest"/>
         <output message="tns:SayHelloResponse"/>
      </operation>
   </portType>

   <binding name="Hello_Binding" type="tns:Hello_PortType">
      <soap:binding style="rpc"
         transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="sayHello">
         <soap:operation soapAction="sayHello"/>
         <input>
            <soap:body
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
               namespace="urn:examples:helloservice"
               use="encoded"/>
         </input>
		
         <output>
            <soap:body
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
               namespace="urn:examples:helloservice"
               use="encoded"/>
         </output>
      </operation>
   </binding>

   <service name="Hello_Service">
      <documentation>WSDL File for HelloService</documentation>
      <port binding="tns:Hello_Binding" name="Hello_Port">
         <soap:address
            location="http://www.examples.com/SayHello/" />
      </port>
   </service>
</definitions>

示例分析

  • Definitions :HelloService

  • Type:使用内置数据类型,它们在XMLSchema中定义。

  • Message

    • sayHelloRequest:firstName参数

    • sayHelloresponse:问候返回值

  • Port Type:由请求和响应服务组成的sayHello 操作

  • Binding:使用SOAP HTTP传输协议的方向。

  • Service:服务可在http://www.examples.com/SayHello/

  • Port:将绑定与URI http://www.examples.com/SayHello/相关联,可以访问正在运行的服务。

 

WSDL元素

1、WSDL<definitions>

所述<definitions>元素必须是所有WSDL文档的根元素。它定义了Web服务的名称。

这是使用定义元素的最后一章的代码片段。

<definitions name="HelloService"
   targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl"
   xmlns="http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   ................................................
</definitions>

从上面的例子可以得出结论:definitions:

  • 是所有其他元素的容器。

  • 指定此文档称为HelloService

  • 指定一个targetNamespace属性。该目标名称是XML模式,使WSDL文档来指代自己的约定。在这个例子中,我们指定了一个http://www.examples.com/wsdl/HelloService.wsdl 的targetNamespace

  • 指定一个默认命名空间:xmlns = http://schemas.xmlsoap.org/wsdl/。因此,没有名称空间前缀的所有元素(如消息portType)都被假定为缺省WSDL命名空间的一部分。

  • 指定在文档的其余部分使用的许多命名空间。

注意:命名空间规范不要求文档存在于给定位置。重要的一点是您指定一个唯一的值,与定义的所有其他命名空间不同。

 

2、WSDL<types>

Web服务需要定义其输入和输出以及它们如何映射到服务中。WSDL <types>元素负责定义Web服务使用的数据类型。类型是XML文档或文档部分。

  • types元素描述了客户端和服务器之间使用的所有数据类型。

  • WSDL并不仅限于特定的打字系统。

  • WSDL使用W3C XML Schema规范作为其默认选择来定义数据类型。

  • 如果服务仅使用XML Schema内置的简单类型(如字符串和整数),则不需要使用types元素。

  • WSDL允许在单独的元素中定义类型,以便这些类型可以重复使用多个Web服务。

这是从W3C规范中获取的一段代码。此代码描述了在WSDL中如何使用类型元素。

<types>
   <schema targetNamespace="http://example.com/stockquote.xsd"
      xmlns="http://www.w3.org/2000/10/XMLSchema">
		
      <element name="TradePriceRequest">
         <complexType>
            <all>
               <element name="tickerSymbol" type="string"/>
            </all>
         </complexType>
      </element>
		
      <element name="TradePrice">
         <complexType>
            <all>
               <element name="price" type="float"/>
            </all>
         </complexType>
      </element>
		
   </schema>
</types>

数据类型解决了识别您要用于Web服务的数据类型和格式的问题。类型信息在发送方和接收方之间共享。因此,消息的接收者需要访问用于对数据进行编码的信息,并且必须了解如何解码数据。

 

3、WSDL<message>

  • 所述<message>元素描述web服务供应商和消费者之间进行交换的数据。

  • 每个Web服务都有两个消息:输入和输出。

  • 输入描述了Web服务的参数,输出描述了来自Web服务的返回数据。

  • 每个消息包含零个或多个<part>参数,一个用于Web服务功能的每个参数。

  • 每个<part>参数与<types>容器元素中定义的具体类型相关联。

让我们从WSDL示例章节中获取一段代码:

<message name="SayHelloRequest">
   <part name="firstName" type="xsd:string"/>
</message>

<message name="SayHelloResponse">
   <part name="greeting" type="xsd:string"/>
</message>

这里定义了两个消息元素。第一个表示一个请求消息SayHelloRequest,第二个表示一个响应消息SayHelloResponse

这些消息中的每一个包含单个零件元素。对于请求,该部分指定函数参数; 在这种情况下,我们指定一个firstName参数。对于响应,该部分指定函数返回值; 在这种情况下,我们指定单个问候返回值。

4、WSDL<portType>

所述<portType>元素组合多个消息元素,以形成一个完整的单向或往返操作。

例如,一个<portType>可以将一个请求和一个响应消息合并到单个请求/响应操作中。这最常用于SOAP服务。portType可以定义多个操作。

让我们从WSDL示例章节中获取一段代码:

<portType name="Hello_PortType">
   <operation name="sayHello">
      <input message="tns:SayHelloRequest"/>
      <output message="tns:SayHelloResponse"/>
   </operation>
</portType>
  • portType元素定义了一个名为sayHello的单个操作。

  • 该操作由单个输入消息SayHelloRequest和一个

  • 输出消息SayHelloResponse

操作模式

WSDL支持四种基本操作模式:

单程

该服务收到消息。因此,该操作具有单个输入元件。单向操作的语法是:

<wsdl:definitions .... > 
   <wsdl:portType .... > *
      <wsdl:operation name="nmtoken">
         <wsdl:input name="nmtoken"? message="qname"/>
      </wsdl:operation>
   </wsdl:portType >
</wsdl:definitions>

请求 - 响应

该服务接收消息并发送响应。因此,该操作具有一个输入元件,随后是一个输出元件。要封装错误,也可以指定可选的故障元素。请求 - 响应操作的语法是:

<wsdl:definitions .... >
   <wsdl:portType .... > *
      <wsdl:operation name="nmtoken" parameterOrder="nmtokens">
         <wsdl:input name="nmtoken"? message="qname"/>
         <wsdl:output name="nmtoken"? message="qname"/>
         <wsdl:fault name="nmtoken" message="qname"/>*
      </wsdl:operation>
   </wsdl:portType >
</wsdl:definitions>

要求响应

该服务发送消息并接收响应。因此,该操作具有一个输出元件,随后是一个输入元件。要封装错误,也可以指定可选的故障元素。请求响应操作的语法是:

<wsdl:definitions .... >
   <wsdl:portType .... > *
      <wsdl:operation name="nmtoken" parameterOrder="nmtokens">
         <wsdl:output name="nmtoken"? message="qname"/>
         <wsdl:input name="nmtoken"? message="qname"/>
         <wsdl:fault name="nmtoken" message="qname"/>*
      </wsdl:operation>
   </wsdl:portType >
</wsdl:definitions>

通知

该服务发送消息。因此,操作具有单个输出元件。以下是通知操作的语法:

<wsdl:definitions .... >
   <wsdl:portType .... > *
      <wsdl:operation name="nmtoken">
         <wsdl:output name="nmtoken"? message="qname"/>
      </wsdl:operation>
   </wsdl:portType >
</wsdl:definitions>

5、WSDL<binding>

所述<binding>元素提供关于如何具体细节的portType操作实际上将通过线路进行传输。

  • 可以通过多个传输(包括HTTP GET,HTTP POST或SOAP)使绑定可用。

  • 绑定提供了有关用于传输portType操作的协议的具体信息。

  • 绑定提供服务所在的信息。

  • 对于SOAP协议,绑定是<soap:binding>,传输是基于HTTP协议的SOAP消息。

  • 您可以为单个portType指定多个绑定。

绑定元素有两个属性:nametype属性。

<binding name="Hello_Binding" type="tns:Hello_PortType">

name属性定义绑定的名称,type属性指向绑定的端口,在本例中为“tns:Hello_PortType”端口。

SOAP绑定

WSDL 1.1包括SOAP 1.1的内置扩展。它允许您指定SOAP特定的详细信息,包括SOAP头,SOAP编码样式和SOAPAction HTTP头。SOAP扩展元素包括以下内容:

  • soap:binding

  • soap:operation

  • soap:body

soap:binding

该元素指示绑定将通过SOAP提供。该风格属性表示SOAP消息格式的整体风格。rpc的样式值指定RPC格式。

传输属性指示SOAP消息的传输。值http://schemas.xmlsoap.org/soap/http表示SOAP HTTP传输,而http://schemas.xmlsoap.org/soap/smtp表示SOAP SMTP传输。

soap:operation

该元素指示特定操作与特定SOAP实现的绑定。所述的soapAction属性指定SOAPAction HTTP头被用于识别该服务。

soap:body

该元素使您能够指定输入和输出消息的详细信息。在HelloWorld的情况下,body元素指定SOAP编码样式和与指定服务关联的命名空间URN。

以下是示例章节中的代码片段:

<binding name="Hello_Binding" type="tns:Hello_PortType">
   <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/ >
   <operation name="sayHello">
      <soap:operation soapAction="sayHello"/>
			
      <input>
         <soap:body
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            namespace="urn:examples:helloservice" use="encoded"/>
      </input>
			
      <output>
         <soap:body
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            namespace="urn:examples:helloservice" use="encoded"/>
      </output>
   </operation>
</binding>

6、WSDL<ports>

一个<port>元素通过指定单个地址的绑定定义单独端点。

以下是指定端口的语法:

<wsdl:definitions .... >
   <wsdl:service .... > *
      <wsdl:port name="nmtoken" binding="qname"> *
         <-- extensibility element (1) -->
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>
  • 端口元素有两个属性:name 和binding

  • name属性在封闭的WSDL文档中定义的所有端口中提供唯一的名称。

  • binding属性是指使用WSDL定义的链接规则的绑定。

  • binding可扩展性元素用于指定端口的地址信息。

  • 一个port不能指定多个地址。

  • 一个port不得指定除地址信息之外的任何绑定信息。

以下是示例章节中的一段代码:

<service name="Hello_Service">
   <documentation>WSDL File for HelloService</documentation>
   <port binding="tns:Hello_Binding" name="Hello_Port">
      <soap:address
         location="http://www.examples.com/SayHello/">
   </port>
</service>

7、WSDL<service>

所述<service>元素定义由Wweb服务支持的端口。对于每个受支持的协议,都有一个端口元素。服务元素是端口的集合。

  • Web服务客户端可以从服务元素中了解以下内容:

    • 在哪里访问服务,

    • 通过哪个端口访问Web服务,以及

    • 通信消息的定义如何。

  • 该服务元素包括一个文档元素,以提供可读的文档。

以下是示例章节中的一段代码:

<service name="Hello_Service">
   <documentation>WSDL File for HelloService</documentation>
   <port binding="tns:Hello_Binding" name="Hello_Port">
      <soap:address
         location="http://www.examples.com/SayHello/">
   </port>
</service>

binding 元素的port属性将服务的地址与Web服务中定义的绑定元素相关联。在这个例子中,这是 Hello_Binding

<binding name="Hello_Binding" type="tns:Hello_PortType">
   <soap:binding style="rpc"
      transport="http://schemas.xmlsoap.org/soap/http"/>
   <operation name="sayHello">
      <soap:operation soapAction="sayHello"/>
		
      <input>
         <soap:body
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            namespace="urn:examples:helloservice" use="encoded"/>
      </input>
			
      <output>
         <soap:body
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            namespace="urn:examples:helloservice" use="encoded"/>
      </output>
   </operation>
</binding>


来自  https://www.cnblogs.com/kqhome/articles/7279714.html



普通分类: