📜  WSDL-快速指南(1)

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

WSDL-快速指南

WSDL (Web Services Description Language) 是一种描述 Web services 的 XML 语言。它可以定义 Web services 的访问地址、参数和返回值等信息。

WSDL 基本结构

WSDL 文档主要由以下几个部分组成:

  • 定义命名空间
  • 定义服务
  • 定义端口类型
  • 定义操作
  • 定义消息类型
定义命名空间

WSDL 文档需要指定 XML 的命名空间,以便描述 Web 服务的元素和类型。

<wsdl:definitions 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    targetNamespace="http://www.example.com/service">
定义服务

服务是一组相关的操作,对外提供 Web 服务。服务可以包含多个端口,每个端口有一个唯一的地址。

<wsdl:service name="MyService">
    <wsdl:port name="MyPort" binding="tns:MyBinding">
        <soap:address location="http://www.example.com/service"/>
    </wsdl:port>
</wsdl:service>
定义端口类型

端口类型定义了端口所支持的操作。每个端口可以支持多个操作。

<wsdl:portType name="MyPortType">
    <wsdl:operation name="Operation1">
        <wsdl:input message="tns:InputMessage"/>
        <wsdl:output message="tns:OutputMessage"/>
        <wsdl:fault name="Fault1" message="tns:FaultMessage"/>
    </wsdl:operation>
    <wsdl:operation name="Operation2">
        <wsdl:input message="tns:InputMessage"/>
    </wsdl:operation>
</wsdl:portType>
定义操作

操作是 Web 服务提供的具体接口。每个操作有输入、输出和错误信息。

<wsdl:message name="InputMessage">
    <wsdl:part name="param1" type="xsd:string"/>
    <wsdl:part name="param2" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="OutputMessage">
    <wsdl:part name="result" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="FaultMessage">
    <wsdl:part name="error" type="xsd:string"/>
</wsdl:message>
定义消息类型

消息类型包含了操作中的每个消息所包含的元素和类型信息。

<wsdl:binding name="MyBinding" type="tns:MyPortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="Operation1">
        <soap:operation soapAction="http://www.example.com/service/Operation1"/>
        <wsdl:input>
            <soap:body use="encoded" namespace="http://www.example.com/service" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
        </wsdl:input>
        <wsdl:output>
            <soap:body use="encoded" namespace="http://www.example.com/service" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
        </wsdl:output>
        <wsdl:fault name="Fault1">
            <soap:fault name="Fault1" use="encoded" namespace="http://www.example.com/service"/>
        </wsdl:fault>
    </wsdl:operation>
</wsdl:binding>
总结

WSDL 是 Web 服务的重要组成部分。了解 WSDL 的基本结构和语法可以帮助程序员更好地理解和设计 Web 服务。相信通过本文的介绍,你已经对 WSDL 有了全面的了解。