WSDL实例解析

WSDL的主要文档元素

WSDL文档可以分为两部分。顶部分由抽象定义组成,而底部分则由具体描述组成。抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随 机器或语言而变的元素。这就定义了一系列服务,截然不同的应用都可以实现。具体部分,如数据的序列化则归入底部分,因为它包含具体的定义。在上述的文档元 素中,<types>、<message>、<portType>属于抽象定义 层,<binding>、<service>属于具体定义层。所有的抽象可以是单独存在于别的文件中,也可以从主文档中导入。 

WSDL文档的结构实例解析 
下面我们将通过一个实际的WSDL文档例子来详细说明各标签的作用及关系。

Wsdl代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <definitions  
  3. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"  
  4. xmlns:tns="http://www.jsoso.com/wstest"  
  5. xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  6. xmlns="http://schemas.xmlsoap.org/wsdl/"  
  7. targetNamespace="http://www.jsoso.com/wstest"  
  8. name="Example">  
  9. <types>  
  10. <xsd:schema>  
  11. <xsd:import  
  12. namespace="http://www.jsoso.com/wstest"  
  13. schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>  
  14. </xsd:schema>  
  15. </types>  
  16. <message name="toSayHello">  
  17. <part name="userName" type="xsd:string"></part>  
  18. </message>  
  19. <message name="toSayHelloResponse">  
  20. <part name="returnWord" type="xsd:string"></part>  
  21. </message>  
  22. <message name="sayHello">  
  23. <part name="person" type="tns:person"></part>  
  24. <part name="arg1" type="xsd:string"></part>  
  25. </message>  
  26. <message name="sayHelloResponse">  
  27. <part name="personList" type="tns:personArray"></part>  
  28. </message>  
  29. <message name="HelloException">  
  30. <part name="fault" element="tns:HelloException"></part>  
  31. </message>  
  32. <portType name="Example">  
  33. <operation name="toSayHello" parameterOrder="userName">  
  34. <input message="tns:toSayHello"></input>  
  35. <output message="tns:toSayHelloResponse"></output>  
  36. </operation>  
  37. <operation name="sayHello" parameterOrder="person arg1">  
  38. <input message="tns:sayHello"></input>  
  39. <output message="tns:sayHelloResponse"></output>  
  40. <fault message="tns:HelloException" name="HelloException"></fault>  
  41. </operation>  
  42. </portType>  
  43. <binding name="ExamplePortBinding" type="tns:Example">  
  44. <soap:binding  
  45. transport="http://schemas.xmlsoap.org/soap/http"  
  46. style="rpc"></soap:binding>  
  47. <operation name="toSayHello">  
  48. <soap:operation soapAction="sayHello"></soap:operation>  
  49. <input>  
  50. <soap:body use="literal"  
  51. namespace="http://www.jsoso.com/wstest"></soap:body>  
  52. </input>  
  53. <output>  
  54. <soap:body use="literal"  
  55. namespace="http://www.jsoso.com/wstest"></soap:body>  
  56. </output>  
  57. </operation>  
  58. <operation name="sayHello">  
  59. <soap:operation soapAction="sayHello"></soap:operation>  
  60. <input>  
  61. <soap:body use="literal"  
  62. namespace="http://www.jsoso.com/wstest"></soap:body>  
  63. </input>  
  64. <output>  
  65. <soap:body use="literal"  
  66. namespace="http://www.jsoso.com/wstest"></soap:body>  
  67. </output>  
  68. <fault name="HelloException">  
  69. <soap:fault name="HelloException" use="literal"></soap:fault>  
  70. </fault>  
  71. </operation>  
  72. </binding>  
  73. <service name="Example">  
  74. <port name="ExamplePort" binding="tns:ExamplePortBinding">  
  75. <soap:address location="http://localhost:8080/hello"></soap:address>  
  76. </port>  
  77. </service>  
  78. </definitions>  

 

由于上面的事例XML较长,我们将其逐段分解讲解

 

WSDL文档的根元素:<definitions>

Xml代码    收藏代码
  1. <definitions  
  2. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"  
  3. xmlns:tns="http://www.jsoso.com/wstest"  
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  5. xmlns="http://schemas.xmlsoap.org/wsdl/"  
  6. targetNamespace="http://www.jsoso.com/wstest"  
  7. name="Example">  
  8. ……  
  9. ……  
  10. </definitions>  

 <definitions>定义了文档中用到的各个xml元素的namespace缩写,也界定了本文档自己的 targetNamespace="http://www.jsoso.com/wstest",这意味着其它的XML要引用当前XML中的元素时,要声 明这个namespace。注意xmlns:tns="http://www.jsoso.com/wstest"这个声明,它标示了使用tns这个前缀 指向自身的命名空间。

 

WSDL文档数据类型定义元素:<types>

Xml代码    收藏代码
  1. <types>  
  2. <xsd:schema>  
  3. <xsd:import  
  4. namespace="http://www.jsoso.com/wstest"  
  5. schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>  
  6. </xsd:schema>  
  7. </types>  

 <types>标签定义了当前的WSDL文档用到的数据类型。要说明的是,为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。这些数据类型用来定义web service方法的参数和返回指。对于通用的原生数据类型如:integer , boolean , char , float等,在W3C的标准文档http://www.w3.org/2001/XMLSchema中已经做了定义。这里我们要引入的schema定义 schemaLocation="http://localhost:8080/hello?xsd=1"是我们自定义的Java对象类型。

 

WSDL文档消息体定义元素:< message >

Xml代码    收藏代码
  1. <message name="toSayHello">  
  2. <part name="userName" type="xsd:string"></part>  
  3. </message>  
  4. <message name="toSayHelloResponse">  
  5. <part name="returnWord" type="xsd:string"></part>  
  6. </message>  
  7. <message name="sayHello">  
  8. <part name="person" type="tns:person"></part>  
  9. <part name="arg1" type="xsd:string"></part>  
  10. </message>  
  11. <message name="sayHelloResponse">  
  12. <part name="personList" type="tns:personArray"></part>  
  13. </message>  
  14. <message name="HelloException">  
  15. <part name="fault" element="tns:HelloException"></part>  
  16. </message>  

 <message>元素定义了web service函数的参数。<message>元素中的每个<part>子元素都和某个参数相符。输入参数在<message>元素中定义,与输出参数相 隔离,输出参数有自己的<message>元素。兼作输入、输出的参数在输入输出的<message>元素中有它们相应的<part>元素。输出 <message>元素以"Response"结尾,对Java而言方法得返回值就对应一个输出的<message>。每个<part>元素都有名字和类 型属性,就像函数的参数有参数名和参数类型。 

在上面的文档中有两个输入参数、两个输出参数和一个错误参数(对应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标签的定义。

 

WSDL文档函数体定义元素:< portType >

Xml代码    收藏代码
  1. <portType name="Example">  
  2. <operation name="toSayHello" parameterOrder="userName">  
  3. <input message="tns:toSayHello"></input>  
  4. <output message="tns:toSayHelloResponse"></output>  
  5. </operation>  
  6. <operation name="sayHello" parameterOrder="person arg1">  
  7. <input message="tns:sayHello"></input>  
  8. <output message="tns:sayHelloResponse"></output>  
  9. <fault message="tns:HelloException" name="HelloException"></fault>  
  10. </operation>  
  11. </portType>  

 
在<operation>元素中,name属性表示服务方法名,parameterOrder属性表示方法的参数顺序,使用空格符分割多个参 数,如:“parameterOrder="person arg1”。<operation>元素的子标签<input>表示输入参数说明,它引用<message>标签中的输入参 数。<output>表示输出参数说明,它引用<message>标签中的输出参数。<fault>标签在Java方法中的特别 用来表示异常(其它语言有对应的错误处理机制),它引用<message>标签中的错误参数。 

WSDL绑定实现定义元素:< binding >

Xml代码    收藏代码
  1. <binding name="ExamplePortBinding" type="tns:Example">  
  2. <soap:binding  
  3. transport="http://schemas.xmlsoap.org/soap/http"  
  4. style="rpc"></soap:binding>  
  5. <operation name="toSayHello">  
  6. <soap:operation soapAction="sayHello"></soap:operation>  
  7. <input>  
  8. <soap:body use="literal"  
  9. namespace="http://www.jsoso.com/wstest"></soap:body>  
  10. </input>  
  11. <output>  
  12. <soap:body use="literal"  
  13. namespace="http://www.jsoso.com/wstest"></soap:body>  
  14. </output>  
  15. </operation>  
  16. <operation name="sayHello">  
  17. <soap:operation soapAction="sayHello"></soap:operation>  
  18. <input>  
  19. <soap:body use="literal"  
  20. namespace="http://www.jsoso.com/wstest"></soap:body>  
  21. </input>  
  22. <output>  
  23. <soap:body use="literal"  
  24. namespace="http://www.jsoso.com/wstest"></soap:body>  
  25. </output>  
  26. <fault name="HelloException">  
  27. <soap:fault name="HelloException" use="literal"></soap:fault>  
  28. </fault>  
  29. </operation>  
  30. </binding>  

 <binding>标签是完整描述协议、序列化和编码的地方,<types>,<message>和<portType>标签处理抽象的数据内容,而<binding>标签是处理数据传输的物理实现。 
<binding>标签把前三部分的抽象定义具体化。 

首先<binding>标签使用<soap:binding>的transport和style属性定义了Web Service的通讯协议HTTP和SOAP的请求风格RPC。其次<operation>子标签将portType中定义的 operation同SOAP的请求绑定,定义了操作名称soapAction,输出输入参数和异常的编码方式及命名空间。 

WSDL服务地址绑定元素:< service >

Xml代码    收藏代码
  1. <service name="Example">  
  2. <port name="ExamplePort" binding="tns:ExamplePortBinding">  
  3. <soap:address location="http://localhost:8080/hello"></soap:address>  
  4. </port>  
  5. </service>  

 service是一套<port>元素。在一一对应形式下,每个<port>元素都和一个location关联。如果同一个<binding>有多个<port>元素与之关联,可以使用额外的URL地址作为替换。 

一个WSDL文档中可以有多个<service>元素,而且多个<service>元素十分有用,其中之一就是可以根据目标URL来组织端口。在一个 WSDL文档中,<service>的name属性用来区分不同的service。在同一个service中,不同端口,使用端口的"name"属性区 分。 

简单的描述了WSDL对SOAP协议的支持,以及在Web Service中的作用。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值