WSDL(Web Services Description Language)是一种用于描述 Web 服务的 XML 格式。WSDL 定义了 Web 服务的接口、消息格式和通信协议,使得不同平台和语言的应用程序能够相互通信。
WSDL 的历史
WSDL 最初由 IBM、微软和其他公司共同开发,旨在提供一种标准的描述 Web 服务的方式。WSDL 1.1 是第一个正式发布的版本,后来又推出了 WSDL 2.0 版本。
WSDL 的结构
WSDL 文档通常包含以下几个重要部分:
- Types:定义 Web 服务使用的数据类型,通常使用 XML Schema 语言来描述。
- Message:定义 Web 服务的消息格式,包括输入消息和输出消息。
- Port Type:定义 Web 服务的操作,包括输入和输出消息的顺序。
- Binding:将抽象的 Port Type 绑定到具体的通信协议和消息格式。
- Service:定义 Web 服务的端点,包括服务的地址和绑定信息。
示例代码
以下是一个简单的 WSDL 示例代码:
// www.javascriptcn.com code example
<definitions name="CalculatorService"
targetNamespace="http://example.com/calculator"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://example.com/calculator">
<types>
<xsd:schema targetNamespace="http://example.com/calculator">
<xsd:element name="add">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="num1" type="xsd:int"/>
<xsd:element name="num2" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="addResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="result" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</types>
<message name="addRequest">
<part name="parameters" element="tns:add"/>
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse"/>
</message>
<portType name="CalculatorPortType">
<operation name="add">
<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="add">
<soap:operation soapAction="http://example.com/calculator/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://example.com/calculator"/>
</port>
</service>
</definitions>以上代码定义了一个简单的计算器 Web 服务,包括一个 add 操作,用于计算两个整数的和。
总结
WSDL 是一种用于描述 Web 服务的标准化格式,通过定义接口、消息和绑定信息,使得不同平台和语言的应用程序能够相互通信。熟练掌握 WSDL 的使用,对于开发和集成 Web 服务应用程序至关重要。