http://www-128.ibm.com/developerworks/cn/webservices/ws-intwsdl/part1/index.html
WSDL 的用途是“描述”您的 Web 服务。业务之间将通过交换 WSDL 文件来理解对方的服务。
WSDL 编写的四个步骤
步骤 1:服务接口
每个 WSDL 文件的根元素都是 <definitions>
,必须在其中提供服务的完整描述。首先,必须在 <definitions>
元素中提供各种名称空间的声明。三个必须做的外部名称空间声明是 WSDL、SOAP 和 XSD(XML 模式定义)。还有一个名称空间 ― TNS,targetNamespace。
<definitions>
元素包含一个或多个 <portType>
元素,实际上,每个元素都是您希望表示的一系列 operation
。或者,您也可以将单个 portType 元素看作是将各种方法组成类的一个逻辑分组。可以将 portType 当作服务,这样整个 WSDL 文件将成为一个服务集合。
清单 1:定义操作
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<portType name="MobilePhoneService_port">
<operation name="getListOfModels ">
.......
.......
</operation>
<operation name="getPrice">
.......
.......
</operation>
</portType>
</definitions>
|
步骤 2:指定参数
定义好operation以后,现在需要指定将向它们发送和从它们返回的参数。在 WSDL 术语中,所有参数称为“消息”message。
Listing 2: Defining parameters
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<types>
<xsd:schema targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns="http://www.w3.org/1999/XMLSchema/">
<xsd:complexType name="Vector">
<xsd:element name="elementData" type="xsd:String" />
<xsd:element name="elementCount" type="xsd:int" />
</xsd:complexType>
</xsd:schema>
</types>
<message name="ListOfPhoneModels">
<part name="models" type="tns:Vector">
</message>
<message name="PhoneModel">
<part name="model" type="xsd:String">
</message>
<message name="PhoneModelPrice">
<part name="price" type="xsd:String">
</message>
<portType name="MobilePhoneService_port">
<operation name="getListOfModels ">
<output message="ListOfPhoneModels"/>
</operation>
<operation name="getPrice">
<Input message="PhoneModel"/>
<output message="PhoneModelPrice"/>
</operation>
</portType>
</definitions>
|
步骤 3:消息传递和传输
WSDL 的任务是定义或描述 Web 服务,然后提供一个对外部框架的引用来定义 WSDL 用户将如何实现这些服务。可以将这个框架当作 WSDL 抽象定义和它们的实现之间的“绑定( binding
)”。
当前,最流行的绑定( binding
)技术是使用简单对象访问协议(SOAP)。WSDL 将指定能够访问 Web 服务实际实现的 SOAP 服务器,并且从那时起 SOAP 的整个任务就是将用户从 WSDL 文件带到它的实现。
第三个步骤是将 SOAP 与 WSDL 文件绑定到一起。您将把 <binding>
元素加入到 <definitions>
元素内。这个 binding 元素应该有 name
和 type
属性。 name
将标识这个绑定而 type
将标识您希望与这个绑定相关联的 portType(一组操作)。
Listing 3: Adding SOAP support
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<types>
<xsd:schema targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns="http://www.w3.org/1999/XMLSchema/">
<xsd:complexType name="Vector">
<xsd:element name="elementData" type="xsd:String" />
<xsd:element name="elementCount" type="xsd:int" />
</xsd:complexType>
</xsd:schema>
</types>
<message name="ListOfPhoneModels">
<part name="models" type="tns:Vector">
</message>
<message name="PhoneModel">
<part name="model" type="xsd:String">
</message>
<message name="PhoneModelPrice">
<part name="price" type="xsd:String">
</message>
<portType name="MobilePhoneService_port">
<operation name="getListOfModels ">
<output message="ListOfPhoneModels"/>
</operation>
<operation name="getPrice">
<Input message="PhoneModel"/>
<output message="PhoneModelPrice"/>
</operation>
</portType>
<binding name="MobilePhoneService_Binding" type="MobilePhoneService_port">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="getListOfModels ">
<soap:operation soapAction="urn:MobilePhoneService" />
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:MobilePhoneService" use="encoded" />
</input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:MobilePhoneService" use="encoded" />
</output>
</operation>
<operation name="getPrice">
<soap:operation soapAction="urn:MobilePhoneService" />
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:MobilePhoneService" use="encoded" />
</input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:MobilePhoneService" use="encoded" />
</output>
</operation>
</binding>
</definitions>
|
步骤 4:概括
创建该 WSDL 文件的概要.
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.mobilephoneservice.com/MobilePhoneService"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<import location="http://localhost:8080/wsdl/MobilePhoneService-interface.wsdl"
namespace="http://www.mobilephoneserviceservice.com/MobilePhoneService-interface" />
<service name="MobilePhoneService">
<documentation> Mobile Phone Information Service </documentation>
<port binding="MobilePhoneService_Binding" name="MobilePhoneService_ServicePort">
<soap:address location="http://localhost:8080/soap/servlet/rpcrouter" />
</port>
</service>
</definitions>
|