WSDL

WSDL(Web Services Description Language)详解

WSDL 是 WebService 的核心描述语言,它以 XML 格式定义了一个 WebService 的功能、数据类型、通信协议、调用地址等关键信息,相当于 WebService 的 “说明书”。客户端通过解析 WSDL,就能知道如何调用服务(比如有哪些方法、参数格式、返回值类型、调用地址等),从而实现跨平台、跨语言的互操作。

WSDL 的核心作用

  • 让客户端明确 WebService 提供的所有可调用功能(如方法名);
  • 定义功能的输入参数、返回值的数据类型;
  • 指定调用服务的协议(如 SOAP over HTTP)和数据格式;
  • 提供服务的网络地址(如http://localhost:8080/...)。

WSDL 构成元素详解(结合示例代码)

WSDL 文档的所有内容都包裹在根元素<definitions>中,其他元素按逻辑分层,形成 “抽象定义” 到 “具体实现” 的完整描述。逐一解析各元素:

1. <definitions>:根元素

  • 作用:作为 WSDL 文档的容器,定义文档的命名空间(避免元素名冲突)和整体元数据。

  • 示例解析:

    <wsdl:definitions  
        targetNamespace="http://com.study.demo/HelloService"  <!-- 该WSDL的唯一命名空间 -->  
        xmlns:tns="http://com.study.demo/HelloService"  <!-- 引用自身命名空间的前缀(tns即"this namespace") -->  
        xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"  <!-- SOAP相关命名空间 -->  
        ...  <!-- 其他标准命名空间(如XML Schema、SOAP编码等) -->  
    >

    命名空间是 WSDL 的核心,确保不同元素(如自定义类型和标准类型)不冲突。

2. <types>:数据类型定义

  • 作用:描述 WebService 与客户端之间交换的所有数据类型(如参数、返回值的格式),通常基于 XML Schema(XSD)定义。

  • 示例解析:

    <wsdl:types>  
        <xsd:schema ... targetNamespace="http://com.study.demo/HelloService">  <!-- 自定义类型的命名空间 -->  
            <!-- 定义"sayHello"方法的输入参数类型:包含一个string类型的"name"参数 -->  
            <xsd:element name="sayHello">  
                <xsd:complexType>  
                    <xsd:sequence>  
                        <xsd:element name="name" type="xsd:string" ... />  
                    </xsd:sequence>  
                </xsd:complexType>  
            </xsd:element>  
            <!-- 定义"sayHello"方法的返回值类型:包含一个string类型的"return"结果 -->  
            <xsd:element name="sayHelloResponse">  
                <xsd:complexType>  
                    <xsd:sequence>  
                        <xsd:element name="return" type="xsd:string" ... />  
                    </xsd:sequence>  
                </xsd:complexType>  
            </xsd:element>  
        </xsd:schema>  
    </wsdl:types>

    这里定义了两个元素:

    sayHello(输入参数)和sayHelloResponse(返回值),后续会被<message>元素引用。

3. <message>:消息定义

  • 作用:表示 WebService 与客户端之间传递的 “逻辑消息”(如请求消息、响应消息),由一个或多个<part>组成(每个<part>对应一个参数或返回值)。

  • 示例解析:

<!-- 定义"sayHello"方法的请求消息:引用types中定义的"sayHello"元素 -->  
<wsdl:message name="sayHelloRequest">  
    <wsdl:part name="parameters" element="tns:sayHello" />  <!-- "tns"指向自定义命名空间 -->  
</wsdl:message>  

<!-- 定义"sayHello"方法的响应消息:引用types中定义的"sayHelloResponse"元素 -->  
<wsdl:message name="sayHelloResponse">  
    <wsdl:part name="parameters" element="tns:sayHelloResponse" />  
</wsdl:message>

每个<message>通过element属性关联<types>中定义的数据类型,明确消息的结构。

4. <portType>:操作定义(服务接口)

  • 作用:定义 WebService 支持的所有操作(即 “方法”),每个操作由 “输入消息”(客户端发送)和 “输出消息”(服务端返回)组成,相当于 “抽象接口”。

  • 示例解析:

    <wsdl:portType name="HelloServicePortType">  <!-- 接口名 -->  
        <wsdl:operation name="sayHello">  <!-- 方法名:sayHello -->  
            <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" />  <!-- 输入消息:请求参数 -->  
            <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" />  <!-- 输出消息:返回值 -->  
        </wsdl:operation>  
    </wsdl:portType>

    这里定义了一个名为sayHello的操作,明确它需要sayHelloRequest作为输入,返回sayHelloResponse。

5. <binding>:绑定协议与格式

  • 作用:将<portType>定义的抽象操作 “绑定” 到具体的通信协议(如 SOAP over HTTP)和数据格式(如 SOAP 消息格式),是 “抽象接口” 到 “具体实现” 的桥梁。

  • 示例解析:

    <wsdl:binding name="HelloServiceHttpBinding" type="tns:HelloServicePortType">  <!-- 绑定到HelloServicePortType接口 -->  
        <!-- 指定协议:SOAP over HTTP -->  
        <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />  
    
        <!-- 为sayHello操作指定SOAP格式 -->  
        <wsdl:operation name="sayHello">  
            <wsdlsoap:operation soapAction="" />  <!-- SOAPAction:可选,用于标识操作 -->  
            <wsdl:input>  
                <wsdlsoap:body use="literal" />  <!-- 消息体使用“文字模式”(直接按XSD定义格式传输) -->  
            </wsdl:input>  
            <wsdl:output>  
                <wsdlsoap:body use="literal" />  
            </wsdl:output>  
        </wsdl:operation>  
    </wsdl:binding>

    这里指定用 SOAP 协议(wsdlsoap:binding),且消息体按<types>中定义的字面格式(use=”literal”)传输。

6. <port>:服务地址

  • 作用:为<binding>绑定的具体协议指定网络地址(即服务的调用 URL)。

  • 示例解析:

    <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding">  <!-- 关联HelloServiceHttpBinding绑定 -->  
        <wsdlsoap:address location="http://localhost:8080/demo/services/HelloService" />  <!-- 服务地址 -->  
    </wsdl:port>

7. <service>:服务集合

  • 作用:聚集一个或多个<port>元素,代表一个完整的 WebService(一个服务可能有多个调用地址或协议)。

  • 示例解析:

    <wsdl:service name="HelloService">  <!-- 服务名 -->  
        <wsdl:port ... />  <!-- 包含一个或多个port(调用地址) -->  
    </wsdl:service>

WSDL 的层次关系

各元素按 “抽象→具体” 分层,关系如下:
types(数据类型)→ message(消息)→ portType(操作接口)→ binding(绑定协议)→ port(地址)→ service(服务集合)

这种层次确保了 WebService 的 “松耦合”:抽象定义(如portType)与具体实现(如协议、地址)分离,便于服务升级或更换协议。

WSDL 的实际用途

  • 客户端通过解析 WSDL,可自动生成调用代码(如 Java 的wsimport工具),无需手动编写 SOAP 消息;
  • 开发者可通过 WSDL 快速了解服务的功能、参数和调用方式,降低集成难度;
  • 支持跨平台 / 跨语言交互(如 Java 服务与 Python 客户端通过 WSDL 对接)