WebService——WSDL详解

作者 : admin 本文共8684个字,预计阅读时间需要22分钟 发布时间: 2023-12-19 共1人阅读

目录

  • WSDL
    • 1、WSDL组成部分
    • 2、WSDL实例
    • 2、definition元素
    • 3、types元素
    • 4、message元素
    • 5、portType元素
      • 5.1、单向操作(one-way)
      • 5.2、请求-响应(request-response)
      • 5.3、询问-响应(solicit-response)
      • 5.4、通知(notification)
    • 6、binding元素
    • 7、ports元素
    • 8、service元素
    • 9、图解

WSDL

WSDL(Web服务描述语言,Web Services Description Language)是为描述Web服务发布的XML格式。W3C组织(World Wide Web Consortium)没有批准1.1版的WSDL,当前的WSDL版本是2.0,是W3C的推荐标准(recommendation)(一种官方标准),并将被W3C组织批准为正式标准。

1、WSDL组成部分

WSDL将Web服务分解为三个特定的,可识别的元素,这些元素可以在定义后组合或重用。可以单独定义的WSDL的三个主要元素是

  • 类型
  • 操作
  • 绑定

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

WSDL文档包含以下元素:

  • Types(消息类型):数据类型定义的容器,它使用某种类型系统(如 XSD)
  • Message(消息):通信数据的抽象类型化定义,它由一个或者多个 part 组成
  • Part:消息参数
  • PortType(端口类型):特定端口类型的具体协议和数据格式规范。,它由一个或者多个 Operation组成
  • Operation(操作):对服务所支持的操作进行抽象描述,WSDL定义了四种操作:
    1. 单向(one-way):端点接收信息;
    2. 请求-响应(request-response):端点接收消息,然后返回响应;
    3. 要求-响应(solicit-response):端点发送消息,然后接收响应;
    4. 通知(notification ):端点发送消息。
  • Binding:特定端口类型的具体协议和数据格式规范。
  • Port:定义为绑定和网络地址组合的单个端点。
  • Service:相关端口的集合,包括其关联的接口、操作、消息等。

2、WSDL实例

假设这个服务提供了一个名称为sayHello的公共可用函数。 此函数需要单个字符串参数并返回单个字符串问候语。 例如,如果传递参数值为:"world",那么服务函数sayHello将返回问候语:"Hello,world!"

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

示例分析说明

  • 定义:HelloService类型 – 使用内置数据类型,它们在XMLSchema中定义。
  • 消息
    • sayHelloRequest – firstName参数
    • sayHelloresponse – 问候的返回值
  • 端口类型:由请求和响应服务组成的sayHello操作。
  • 绑定:使用SOAP HTTP传输协议的方向。
  • 服务: 可从 http://www.examples.com/SayHello/ 获取服务
  • 端口:将绑定与URI =>http://www.examples.com/SayHello/ 相关联,可以访问正在运行的服务。

2、definition元素

WSDL 元素必须是所有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>

从上面的例子中,可以知道:

  • 是所有其他元素的容器
  • 指定此文档名为:HelloService
  • 指定targetNamespace属性,targetNamespace是XML Schema的约定,它使WSDL文档能够引用自身。在此示例中,我们指定了一个: http://www.examples.com/wsdl/HelloService.wsdltargetNamespace
  • 指定默认命名空间:xmlns=http://schemas.xmlsoap.org/wsdl/ 。 因此,假定所有没有名称空间前缀的元素(如message或portType)都是默认WSDL名称空间的一部分。
  • 指定在整个文档的其余部分中使用的其它名称空间

3、types元素

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

  • types元素描述客户端和服务器之间使用的所有数据类型。
  • WSDL并不专门针对特定的输入系统。
  • WSDL使用W3C XML Schema规范作为定义数据类型的默认选择。
  • 如果服务仅使用XML Schema内置的简单类型(如字符串和整数),则不需要types元素。
  • WSDL允许在单独的元素中定义类型,以便可以使用多个Web服务重用这些类型。

下面是一段取自W3C规范的代码,此代码描述了如何在WSDL中使用types元素:

<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服务一起使用的格式的问题。 类型信息在发送方和接收方之间共享。 因此,消息的接收者需要访问用于编码数据的信息,并且必须了解如何解码数据。

4、message元素

WSDL 元素描述了Web服务生产者和消费者之间交换的数据。

  • 每个Web服务都有两条消息:输入和输出。
  • 输入描述Web服务的参数,输出描述Web服务的返回数据。
  • 每条消息包含零个或多个参数,每个参数对应一个Web服务函数的参数。
  • 每个参数与容器元素中定义的具体类型相关联。
<message name = "SayHelloRequest">
<part name = "firstName" type = "xsd:string"/>
</message>
<message name = "SayHelloResponse">
<part name = "greeting" type = "xsd:string"/>
</message>

这里定义了两个消息元素:

  • 第一个表示请求消息SayHelloRequest
  • 第二个表示响应消息SayHelloResponse

这些消息中都包含一个元素。 对于请求,指定函数参数;

在这个示例中,指定一个firstName参数。 对于响应指定函数返回值; 在这个示例中,指定一个问候语(greeting)返回值。

5、portType元素

WSDL 元素组合了多个消息()元素,以形成完整的单向或往返操作。

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

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

下面是对上面示例代码的解释说明:

  • portType元素定义了一个名称为sayHello的操作。
  • 该操作由单个输入消息SayHelloRequest和一个输出消息SayHelloResponse组成。

5.1、单向操作(one-way)

该服务收到一条消息。 因此,操作具有单个input元素。 单向操作的语法是:

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

5.2、请求-响应(request-response)

该服务接收消息并发送响应。 因此,操作有一个input元素,后跟一个output元素。 要封装错误,还可以指定可选的fault元素。 请求-响应操作的语法是:

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

5.3、询问-响应(solicit-response)

该服务发送消息并接收响应。 因此,操作有一个output元素,后跟一个input元素。 要封装错误,还可以指定可选的fault元素。 询问响应操作的语法是:

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

5.4、通知(notification)

该服务发送一条消息。 因此,操作具有单个input元素。 以下是通知操作的语法:

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

6、binding元素

WSDL 元素提供了有关如何通过线路传输portType实际操作的具体细节:

  • 绑定可以通过多种传输方式提供,包括HTTP GET,HTTP POST或SOAP。
  • 绑定提供了有关用于传输portType操作的协议的具体信息。
  • 绑定提供服务所在的信息。
  • 对于SOAP协议,绑定是使用,表示传输是基于HTTP协议的SOAP消息。
  • 可以为单个portType指定多个绑定。

绑定元素有两个属性:name和type属性:

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

在上面示例代码中,name属性定义绑定的名称,type属性指向绑定的端口,在本例中为tns:Hello_PortType端口。

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

  • soap:binding
  • soap:operation
  • soap:body

sop:binding:
此元素表示将通过SOAP提供绑定。 style属性指示SOAP消息格式的整体样式。 style的值rpc指定RPC格式。

transport属性指示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:
此元素用于指定输入和输出消息的详细信息。 对于HelloWorldbody元素指定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>

7、ports元素

WSDL 元素通过为绑定指定单个地址来定义单个端点。
这是指定端口的语法:

<wsdl:definitions .... >
<wsdl:service .... > *
<wsdl:port name = "nmtoken" binding = "qname"> *
<-- extensibility element (1) -->
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
  • port元素有两个属性:namebinding
  • name属性在封闭的WSDL文档中定义的所有端口中提供唯一名称。
  • binding属性是指使用WSDL定义的链接规则进行绑定。
  • 绑定可扩展性元素用于指定端口的地址信息。
  • 端口不得指定多个地址。
  • 端口不得指定除地址信息之外的任何绑定信息。
<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>

8、service元素

WSDL 元素定义Web服务支持的端口。 对于每个支持的协议,都有一个元素。 service元素是端口的集合。

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

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>

9、图解

WebService——WSDL详解插图

WebService——WSDL详解插图(1)

本站无任何商业行为
个人在线分享 » WebService——WSDL详解
E-->