
WSDL文档的结构实例解析
下面我们将通过一个实际的WSDL文档例子来详细说明各标签的作用及关系。
- <?xml version="1.0" encoding="UTF-8"?>
- <definitions
- xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
- xmlns:tns="http://www.jsoso.com/wstest"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns="http://schemas.xmlsoap.org/wsdl/"
- targetNamespace="http://www.jsoso.com/wstest"
- name="Example">
- <types>
- <xsd:schema>
- <xsd:import
- namespace="http://www.jsoso.com/wstest"
- schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
- </xsd:schema>
- </types>
- <message name="toSayHello">
- <part name="userName" type="xsd:string"></part>
- </message>
- <message name="toSayHelloResponse">
- <part name="returnWord" type="xsd:string"></part>
- </message>
- <message name="sayHello">
- <part name="person" type="tns:person"></part>
- <part name="arg1" type="xsd:string"></part>
- </message>
- <message name="sayHelloResponse">
- <part name="personList" type="tns:personArray"></part>
- </message>
- <message name="HelloException">
- <part name="fault" element="tns:HelloException"></part>
- </message>
- <portType name="Example">
- <operation name="toSayHello" parameterOrder="userName">
- <input message="tns:toSayHello"></input>
- <output message="tns:toSayHelloResponse"></output>
- </operation>
- <operation name="sayHello" parameterOrder="person arg1">
- <input message="tns:sayHello"></input>
- <output message="tns:sayHelloResponse"></output>
- <fault message="tns:HelloException" name="HelloException"></fault>
- </operation>
- </portType>
- <binding name="ExamplePortBinding" type="tns:Example">
- <soap:binding
- transport="http://schemas.xmlsoap.org/soap/http"
- style="rpc"></soap:binding>
- <operation name="toSayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- </operation>
- <operation name="sayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- <fault name="HelloException">
- <soap:fault name="HelloException" use="literal"></soap:fault>
- </fault>
- </operation>
- </binding>
- <service name="Example">
- <port name="ExamplePort" binding="tns:ExamplePortBinding">
- <soap:address location="http://localhost:8080/hello"></soap:address>
- </port>
- </service>
- </definitions>
- <definitions
- xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
- xmlns:tns="http://www.jsoso.com/wstest"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns="http://schemas.xmlsoap.org/wsdl/"
- targetNamespace="http://www.jsoso.com/wstest"
- name="Example">
- ……
- ……
- </definitions>
- <types>
- <xsd:schema>
- <xsd:import
- namespace="http://www.jsoso.com/wstest"
- schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
- </xsd:schema>
- </types>
- <message name="toSayHello">
- <part name="userName" type="xsd:string"></part>
- </message>
- <message name="toSayHelloResponse">
- <part name="returnWord" type="xsd:string"></part>
- </message>
- <message name="sayHello">
- <part name="person" type="tns:person"></part>
- <part name="arg1" type="xsd:string"></part>
- </message>
- <message name="sayHelloResponse">
- <part name="personList" type="tns:personArray"></part>
- </message>
- <message name="HelloException">
- <part name="fault" element="tns:HelloException"></part>
- </message>
在上面的文档中有两个输入参数、两个输出参数和一个错误参数(对应Java中的Exception)。
输入参数<message>的name属性分别命名为toSayHello,sayHello。
toSayHello对应输入参数userName,参数类型为xsd:string,在Java语言中就是String;
sayHello对应两个输入参数person和arg1,类型为tns:person和xsd:string。这里tns:person类型就是引用了< types >标签中的类型定义。
输出参数<message>的name属性分别命名为toSayHelloResponse和sayHelloResponse。
这个名称和输入参数的<message>标签name属性对应,在其后面加上Response尾缀。
toSayHelloResponse对应的返回值是returnWord,参数类型为xsd:string;
sayHelloResponse对应的返回值是personList,参数类型为tns:personArray(自定义类型);
错误参数<message>的name属性为HelloException。
它的<part>子标签element而不是type来定义类型。
以上的message标签的name属性通常使用web service函数方法名作为参照,错误参数标签则使用异常类名为参照。标签中的参数名称,即part子元素的name属性是可自定义的(下一章节详细说明)。message标签的参数类型将引用types标签的定义。
- <portType name="Example">
- <operation name="toSayHello" parameterOrder="userName">
- <input message="tns:toSayHello"></input>
- <output message="tns:toSayHelloResponse"></output>
- </operation>
- <operation name="sayHello" parameterOrder="person arg1">
- <input message="tns:sayHello"></input>
- <output message="tns:sayHelloResponse"></output>
- <fault message="tns:HelloException" name="HelloException"></fault>
- </operation>
- </portType>
在<operation>元素中,name属性表示服务方法名,parameterOrder属性表示方法的参数顺序,使用空格符分割多个参数,如:“parameterOrder="person arg1”。<operation>元素的子标签<input>表示输入参数说明,它引用<message>标签中的输入参数。<output>表示输出参数说明,它引用<message>标签中的输出参数。<fault>标签在Java方法中的特别用来表示异常(其它语言有对应的错误处理机制),它引用<message>标签中的错误参数。
WSDL绑定实现定义元素:< binding >
- <binding name="ExamplePortBinding" type="tns:Example">
- <soap:binding
- transport="http://schemas.xmlsoap.org/soap/http"
- style="rpc"></soap:binding>
- <operation name="toSayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- </operation>
- <operation name="sayHello">
- <soap:operation soapAction="sayHello"></soap:operation>
- <input>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </input>
- <output>
- <soap:body use="literal"
- namespace="http://www.jsoso.com/wstest"></soap:body>
- </output>
- <fault name="HelloException">
- <soap:fault name="HelloException" use="literal"></soap:fault>
- </fault>
- </operation>
- </binding>
<binding>标签把前三部分的抽象定义具体化。
首先<binding>标签使用<soap:binding>的transport和style属性定义了Web Service的通讯协议HTTP和SOAP的请求风格RPC。其次<operation>子标签将portType中定义的 operation同SOAP的请求绑定,定义了操作名称soapAction,输出输入参数和异常的编码方式及命名空间。
WSDL服务地址绑定元素:< service >
- <service name="Example">
- <port name="ExamplePort" binding="tns:ExamplePortBinding">
- <soap:address location="http://localhost:8080/hello"></soap:address>
- </port>
- </service>
一个WSDL文档中可以有多个<service>元素,而且多个<service>元素十分有用,其中之一就是可以根据目标URL来组织端口。在一个 WSDL文档中,<service>的name属性用来区分不同的service。在同一个service中,不同端口,使用端口的"name"属性区分。
简单的描述了WSDL对SOAP协议的支持,以及在Web Service中的作用。