📜  WSDL-元素(1)

📅  最后修改于: 2023-12-03 15:35:45.254000             🧑  作者: Mango

WSDL 元素

WSDL(Web Services Description Language)描述了 Web 服务的接口,其中包含有关服务以及如何调用它的详细信息。WSDL 中的元素提供了描述具体 Web 服务的详细信息,使客户端程序员能够理解如何与该服务进行交互。

WSDL 元素的类型

WSDL 中包含多种元素,但所有元素均可分为以下四种类型:

1. 定义元素

定义元素是 Web 服务的基础构建块之一,它描述了 Web 服务的抽象类型和消息类型。WSDL 定义元素包括 typesmessageportType。其中,types 元素包含有关 Web 服务的数据类型和消息格式的信息,message 元素定义 Web 服务的消息结构,而 portType 元素用于描述完整的请求-响应操作。

2. 绑定元素

绑定元素用于绑定协议和消息格式,以便客户端可以对 Web 服务进行调用。绑定元素包括 bindingoperation

3. 服务元素

服务元素包括 service 元素和 port 元素。它们描述了 Web 服务的物理位置和提供商,并将绑定元素与端点地址相关联。

4. 其他元素

其他元素包括 importdocumentation 等元素。import 元素允许将 WSDL 文件分成多个文件,documentation 元素用于提供有关 Web 服务的额外信息。

一个例子

下面是一个简单的 WSDL 文件示例,其中包括了上面提到的所有元素:

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

  <types>
    <xsd:schema targetNamespace="http://www.example.com/wsdl">
      <xsd:element name="getRequest">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="getResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="message" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </types>

  <message name="HelloRequest">
    <part name="parameters" element="tns:getRequest"/>
  </message>

  <message name="HelloResponse">
    <part name="parameters" element="tns:getResponse"/>
  </message>

  <portType name="HelloPortType">
    <operation name="sayHello">
      <input message="tns:HelloRequest"/>
      <output message="tns:HelloResponse"/>
    </operation>
  </portType>

  <binding name="HelloBinding" type="tns:HelloPortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="sayHello">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>

  <service name="HelloService">
    <port name="HelloPort" binding="tns:HelloBinding">
      <soap:address location="http://localhost:8080/Hello"/>
    </port>
  </service>

</definitions>
总结

WSDL 是控制 Web 服务交互的重要元素之一。WSDL 中的元素都有其作用和目的,它们描述了 Web 服务的接口、请求/响应和物理位置等信息,使得客户端程序员能够理解如何与 Web 服务进行交互。