WSDL 语法学习笔记
本文将介绍 WSDL 的语法,以及使用 WSDL 的示例。
什么是 WSDL?
WSDL(Web Services Description Language)是一种用于描述 Web 服务的语言。它提供了一种机制,使得客户端可以了解 Web 服务提供商所提供的服务,以及如何访问这些服务。
WSDL 的语法
WSDL 使用 XML 来描述 Web 服务。它由以下几个部分组成:
types
types 部分定义了 Web 服务中可能使用的数据类型。例如,可以定义一个名为 Person 的复合类型,该类型包含 name 和 age 两个字段。
Copy Code<types>
<xsd:schema targetNamespace="http://example.com">
<xsd:complexType name="Person">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>
message
message 部分定义了 Web 服务中使用的消息。一个消息由一个或多个 part 组成。每个 part 可以引用 types 部分定义的数据类型。
Copy Code<message name="GetPersonRequest">
<part name="personId" type="xsd:string"/>
</message>
<message name="GetPersonResponse">
<part name="person" type="tns:Person"/>
</message>
portType
portType 部分定义了 Web 服务的接口。它是一个由一组操作组成的抽象集合。每个操作由一个唯一名称、一个输入消息和一个输出消息组成。
Copy Code<portType name="PersonService">
<operation name="GetPerson">
<input message="tns:GetPersonRequest"/>
<output message="tns:GetPersonResponse"/>
</operation>
</portType>
binding
binding 部分定义了 portType 的具体实现方式。它指定了协议和传输格式,以及 portType 中各个操作的详细信息。
Copy Code<binding name="PersonServiceBinding" type="tns:PersonService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetPerson">
<soap:operation soapAction="http://example.com/GetPerson"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
service
service 部分定义了 Web 服务的具体地址。它引用了 binding 部分中定义的具体实现。
Copy Code<service name="PersonService">
<port name="PersonServicePort" binding="tns:PersonServiceBinding">
<soap:address location="http://example.com/PersonService"/>
</port>
</service>
WSDL 示例
下面是一个简单的 WSDL 示例,它定义了一个名为 PersonService 的 Web 服务,该服务具有一个名为 GetPerson 的操作。
Copy Code<?xml version="1.0" encoding="UTF-8"?>
<definitions name="PersonService"
targetNamespace="http://example.com"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://example.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
>
<types>
<xsd:schema targetNamespace="http://example.com">
<xsd:complexType name="Person">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>
<message name="GetPersonRequest">
<part name="personId" type="xsd:string"/>
</message>
<message name="GetPersonResponse">
<part name="person" type="tns:Person"/>
</message>
<portType name="PersonService">
<operation name="GetPerson">
<input message="tns:GetPersonRequest"/>
<output message="tns:GetPersonResponse"/>
</operation>
</portType>
<binding name="PersonServiceBinding" type="tns:PersonService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetPerson">
<soap:operation soapAction="http://example.com/GetPerson"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="PersonService">
<port name="PersonServicePort" binding="tns:PersonServiceBinding">
<soap:address location="http://example.com/PersonService"/>
</port>
</service>
</definitions>
该示例定义了一个名为 Person 的复合类型,包含 name 和 age 两个字段。它还定义了一个 GetPerson 的操作,该操作接受一个名为 personId 的字符串参数,并返回一个包含 Person 类型的对象。
结论
本文介绍了 WSDL 的语法,以及使用 WSDL 的示例。WSDL 提供了一种方便的机制,使得 Web 服务的客户端可以了解 Web 服务提供商所提供的服务,以及如何访问这些服务。