WSDL 文档学习笔记
什么是WSDL
WSDL (Web Services Description Language) 是一种用于描述 Web 服务的 XML 文档格式。它定义了 Web 服务的接口、操作和消息,并指定了如何访问 Web 服务。
WSDL 的基本结构
xmlCopy Code<definitions name="serviceName"
targetNamespace="http://www.example.org/ServiceName/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/ServiceName/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/">
<types>
<!-- 定义数据类型(可选) -->
</types>
<message>
<!-- 定义消息(可选) -->
</message>
<portType>
<!-- 定义端口类型,包含一组相关的操作(必需) -->
</portType>
<binding>
<!-- 绑定端口类型和通信协议,定义如何访问 Web 服务(必需) -->
</binding>
<service>
<!-- 定义 Web 服务的名称和网络位置(必需) -->
</service>
</definitions>
实例解析
假设我们有一个简单的 Web 服务,可以将两个整数相加并返回和。现在,我们来编写一个 WSDL 文档来描述这个 Web 服务。
- 首先,我们定义数据类型,在
<types>
元素内添加如下内容:
xmlCopy Code<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/addition/"
xmlns:tns="http://www.example.org/addition/">
<xs:element name="add">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:int"/>
<xs:element name="b" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="addResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="result" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
- 接着,我们定义消息,在
<message>
元素内添加如下内容:
xmlCopy Code<message name="addRequest">
<part element="tns:add" name="parameters"/>
</message>
<message name="addResponse">
<part element="tns:addResponse" name="parameters"/>
</message>
- 然后,我们定义端口类型,在
<portType>
元素内添加如下内容:
xmlCopy Code<portType name="AdditionPortType">
<operation name="AdditionOperation">
<input message="tns:addRequest"/>
<output message="tns:addResponse"/>
</operation>
</portType>
- 绑定端口类型和通信协议,在
<binding>
元素内添加如下内容:
xmlCopy Code<binding name="AdditionBinding" type="tns:AdditionPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="AdditionOperation">
<soap:operation soapAction="http://www.example.org/addition/AdditionOperation"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
- 最后,我们定义 Web 服务的名称和网络位置,在
<service>
元素内添加如下内容:
xmlCopy Code<service name="AdditionService">
<port binding="tns:AdditionBinding" name="AdditionPort">
<soap:address location="http://localhost:8080/AdditionService"/>
</port>
</service>
这样,我们就完成了一个简单的 WSDL 文档。保存为 AdditionService.wsdl
文件,并与 Web 服务一起发布。其他应用程序可以使用此 WSDL 文档来访问该 Web 服务。