WSDL 学习笔记

什么是 WSDL?

WSDL(Web Services Description Language)是一种 XML 文档,用于描述 Web 服务的接口。它定义了 Web 服务提供者向外界公开的接口信息,包括输入参数、输出参数、方法等。WSDL 可以作为客户端与服务端之间的通信协议来使用。

WSDL 的结构

WSDL 文件由四个部分组成:

  1. types:定义消息中使用的数据类型。

  2. message:定义在 Web 服务中传递的消息。

  3. portType:定义 Web 服务中可以调用的操作,即 Web 服务的接口。

  4. binding:将端口类型映射到协议和消息格式。

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

xmlCopy Code
<?xml version="1.0" encoding="UTF-8"?> <definitions name="HelloService" targetNamespace="http://example.com/HelloService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://example.com/HelloService" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <types> <xsd:schema targetNamespace="http://example.com/HelloService"> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="greeting" type="xsd:string"/> </xsd:schema> </types> <message name="SayHelloRequest"> <part name="firstName" element="tns:firstName"/> <part name="lastName" element="tns:lastName"/> </message> <message name="SayHelloResponse"> <part name="greeting" element="tns:greeting"/> </message> <portType name="HelloPort"> <operation name="sayHello"> <input message="tns:SayHelloRequest"/> <output message="tns:SayHelloResponse"/> </operation> </portType> <binding name="HelloBinding" type="tns:HelloPort"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="sayHello"> <soap:operation soapAction="http://example.com/sayHello"/> <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://example.com/HelloService"/> </port> </service> </definitions>

我们可以看到这个 WSDL 文件定义了一个名为 HelloService 的 Web 服务,并提供了一个 sayHello 方法。该方法接收两个参数:firstName 和 lastName,返回一个 greeting 字符串。

WSDL 的实例

假设我们有一个简单的 Web 服务,它接收一个字符串类型的参数并返回对应字符串的长度。现在我们需要使用 WSDL 来描述这个 Web 服务。

首先,我们需要定义一个 xsd:schema,用于定义数据类型。在这个例子中,我们只需要定义一个字符串类型。

xmlCopy Code
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/StringLengthService"> <xsd:element name="input" type="xsd:string"/> </xsd:schema>

接着,我们需要定义消息。在本例中,我们只需要一个包含一个 input 参数的消息。

xmlCopy Code
<message name="GetStringLengthRequest"> <part name="input" element="tns:input"/> </message>

我们还需要定义一个返回字符串长度的消息。

xmlCopy Code
<message name="GetStringLengthResponse"> <part name="result" type="xsd:int"/> </message>

下一步是定义端口类型。需要定义一个名为 StringLengthPort 的端口类型,并在其中定义一个名为 getStringLength 的操作。getStringLength 操作接收 GetStringLengthRequest 消息作为输入,返回 GetStringLengthResponse 消息作为输出。

xmlCopy Code
<portType name="StringLengthPort"> <operation name="getStringLength"> <input message="tns:GetStringLengthRequest"/> <output message="tns:GetStringLengthResponse"/> </operation> </portType>

接着,我们需要定义如何将端口类型映射到协议和消息格式。在本例中,我们将使用 SOAP 协议和 HTTP 传输。

xmlCopy Code
<binding name="StringLengthBinding" type="tns:StringLengthPort"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="getStringLength"> <soap:operation soapAction="http://example.com/GetStringLength"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding>

最后,我们需要定义一个服务,并将端口映射到服务上。这里我们可以使用本地地址作为服务地址。

xmlCopy Code
<service name="StringLengthService"> <port name="StringLengthPort" binding="tns:StringLengthBinding"> <soap:address location="http://localhost/StringLengthService"/> </port> </service>

现在我们已经完成了这个 Web 服务的 WSDL 描述。我们可以将这个 WSDL 文件提供给客户端,让客户端使用 SOAP 消息来调用该服务。

总的来说,WSDL 是一种非常有用的格式,用于描述 Web 服务接口和数据类型。它可以帮助开发人员更好地理解和使用 Web 服务,从而提高开发效率和质量。