WSDL 简介学习笔记

概述

WSDL(Web Services Description Language)是一种基于 XML 的描述 Web 服务的语言。它定义了 Web 服务的接口、消息格式、通信协议和网络地址等细节,使得客户端能够清楚地了解如何与服务端进行交互。

WSDL 的核心元素

WSDL 文件主要包括以下核心元素:

  1. types:定义 Web 服务中所使用的数据类型。

  2. message:定义 Web 服务的方法所使用的数据格式。

  3. portType:定义 Web 服务中可用的所有方法,每个方法都由 input 和 output 组成。

  4. binding:定义 Web 服务的具体协议和传输方式。

  5. service:定义 Web 服务的网络地址和提供的方法列表。

WSDL 实例

以下是一个简单的 WSDL 文件示例:

xmlCopy Code
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://www.example.com/calculator" name="CalculatorService"> <types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="int1" type="xsd:int"/> <xsd:element name="int2" type="xsd:int"/> <xsd:element name="result" type="xsd:int"/> </xsd:schema> </types> <message name="AddRequest"> <part name="int1" element="tns:int1"/> <part name="int2" element="tns:int2"/> </message> <message name="AddResponse"> <part name="result" element="tns:result"/> </message> <portType name="CalculatorPortType"> <operation name="AddOperation"> <input message="tns:AddRequest"/> <output message="tns:AddResponse"/> </operation> </portType> <binding name="CalculatorBinding" type="tns:CalculatorPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="AddOperation"> <soap:operation soapAction="urn:add"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="CalculatorService"> <port name="CalculatorPort" binding="tns:CalculatorBinding"> <soap:address location="http://www.example.com/calculator"/> </port> </service> </definitions>

该 WSDL 文件定义了一个名为 CalculatorService 的 Web 服务,提供了一个 AddOperation 的方法,用于计算两个整数的和。该方法的 input 和 output 分别使用了 AddRequest 和 AddResponse 消息,其中 AddRequest 包含两个整数参数,而 AddResponse 只包含一个整数结果参数。

在 binding 部分,定义了使用 SOAP 协议进行通信,并指定了通信地址为 http://www.example.com/calculator。

总结

WSDL 是一种描述 Web 服务的语言,它能够帮助客户端了解 Web 服务的接口、消息格式、通信协议和网络地址等细节。通过上述实例,我们可以更加清晰地了解 WSDL 文件的结构和用法,有助于我们更好地理解和开发 Web 服务。