WSDL 教程学习笔记

什么是 WSDL?

WSDL(Web Services Description Language)是用于描述 Web 服务接口的 XML 文件。它定义了 Web 服务的输入、输出和所支持的操作。

WSDL 的组成部分

WSDL 由以下几个部分组成:

  • Types:定义 Web 服务使用的 XML 数据类型,例如字符串和整数等。
  • Message:定义 Web 服务方法的输入和输出参数。
  • PortType:定义一组相关的 Web 服务方法。
  • Binding:定义 Web 服务使用的协议和格式。
  • Service:定义 Web 服务实现的位置和运行方式。

WSDL 实例

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

xmlCopy Code
<?xml version="1.0" encoding="UTF-8"?> <definitions name="MyService" targetNamespace="http://www.example.com/myservice/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.com/myservice/"> <types> <xsd:schema targetNamespace="http://www.example.com/myservice/"> <xsd:element name="input" type="xsd:string"/> <xsd:element name="output" type="xsd:string"/> </xsd:schema> </types> <message name="MyMethodRequest"> <part name="input" element="tns:input"/> </message> <message name="MyMethodResponse"> <part name="output" element="tns:output"/> </message> <portType name="MyServicePortType"> <operation name="MyMethod"> <input message="tns:MyMethodRequest"/> <output message="tns:MyMethodResponse"/> </operation> </portType> <binding name="MyServiceBinding" type="tns:MyServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="MyMethod"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="MyService"> <port name="MyServicePort" binding="tns:MyServiceBinding"> <soap:address location="http://www.example.com/myservice/"/> </port> </service> </definitions>

该 WSDL 文件定义了 MyService Web 服务接口,使用 HTTP 作为传输协议。它包括以下部分:

  • Types:定义了输入和输出参数的数据类型,此处为字符串。
  • Message:定义了 Web 服务方法的输入和输出消息。此处定义的是 MyMethod 方法,它有一个名为 input 的参数和一个名为 output 的返回值。
  • PortType:定义了一组相关的 Web 服务方法,此处定义了 MyMethod 方法。
  • Binding:定义了使用 SOAP 协议进行通信时所需的绑定和格式。此处定义的是 MyServiceBinding 使用文档样式的 SOAP 格式。
  • Service:定义了 MyService 实现的位置和运行方式。此处定义了 MyServicePort 使用 MyServiceBinding 访问。

总结

WSDL 是用于描述 Web 服务接口的 XML 文件,包括 Types、Message、PortType、Binding 和 Service 等部分。它提供了一种标准化的方式来描述 Web 服务,使得不同平台和语言的应用程序可以相互通信。