webservice-WSDL结构与各元素解析

    最近一直 在使用公司的框架开发webservice服务接口,于是决定好好了解下webService。

      关于webservice的定义,网上一搜一大堆,但都大同小异,个人感觉不好理解。根据自己开发的经验,个人认为webservice就是通过网络暴露程序API,供其他应用调用实现RPC。所谓通过网络暴露,说通俗点就是就好比一般的web应用中浏览器向后台发送一个指定的交易请求,服务器会根据请求内容调用相关方法处理。不同的是,一般web应用客户端与服务端使用一种语言(比如Java中jsp与servlet),而webservice能够做到跨语言,跨平台调用。

      完成webservice最重要的两个元素是wsdl与soap。

    wsdl的全称是web service Description Language,是一种基于XML格式的关于web服务的描述语言。其主要目的在于webservice的提供者将自己的web服务的所有  相关内容,如所提供的服务的传输方式,服务方法接口,接口参数,服务路径等,生成相应的完全文档,发布给使用者。使用者可以通过这个wsdl文档,创建相应 的SOAP请求消息,通过HTTP传递给webservice提供者;web服务在完成服务请求后,将SOAP返回消息传回请求者,服务请求者再根据wsdl文档将SOAP返回消息解析成自己能够理解的内容。但是,wsdl 并不是完全为传输soap消息而出现的,它也可以用于描述基于其他协议的服务。如:HTTP-GET/POST 或者是SMTP等。下面是一个helloworld的例子:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"  
  3.     xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  4.     xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"  
  5.     name="HelloWorldService">  
  6.     <types>  
  7.         <xs:schema xmlns:tns="http://example.helloworld.com/"  
  8.             xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"  
  9.             targetNamespace="http://example.helloworld.com/">  
  10.             <xs:element name="sayHello">  
  11.                 <xs:complexType>  
  12.                     <xs:sequence>  
  13.                         <xs:element name="arg0" type="xs:string" minOccurs="0" />  
  14.                     </xs:sequence>  
  15.                 </xs:complexType>  
  16.             </xs:element>  
  17.             <xs:element name="sayHelloResponse">  
  18.                 <xs:complexType>  
  19.                     <xs:sequence>  
  20.                         <xs:element name="return" type="xs:string" minOccurs="0" />  
  21.                     </xs:sequence>  
  22.                 </xs:complexType>  
  23.             </xs:element>  
  24.         </xs:schema>  
  25.     </types>  
  26.     <message name="sayHello">  
  27.         <part name="parameters" element="tns:sayHello"></part>  
  28.     </message>  
  29.     <message name="sayHelloResponse">  
  30.         <part name="parameters" element="tns:sayHelloResponse"></part>  
  31.     </message>  
  32.     <portType name="HelloWorld">  
  33.         <operation name="sayHello">  
  34.             <input message="tns:sayHello"></input>  
  35.             <output message="tns:sayHelloResponse"></output>  
  36.         </operation>  
  37.     </portType>  
  38.     <binding name="HelloWorldPortBinding" type="tns:HelloWorld">  
  39.         <soap:binding transport="http://schemas.xmlsoap.org/soap/http"  
  40.             style="document"></soap:binding>  
  41.         <operation name="sayHello">  
  42.             <soap:operation soapAction=""></soap:operation>  
  43.             <input>  
  44.                 <soap:body use="literal"></soap:body>  
  45.             </input>  
  46.             <output>  
  47.                 <soap:body use="literal"></soap:body>  
  48.             </output>  
  49.         </operation>  
  50.     </binding>  
  51.     <service name="HelloWorldService">  
  52.         <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">  
  53.             <soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>  
  54.         </port>  
  55.     </service>  
  56. </definitions>  

   soap是web service的标准通信协议,soap为simple object access protocoll的缩写,简单对象访问协议. 它是一种标准化的传输消息的XML消息格式。

   调用上面hellowolrd.wsdl生成客户端后调用服务发送与接收的soap报文如下:

   output:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
  3.     <soap:Body>  
  4.         <ns2:sayHello xmlns:ns2="http://example.helloworld.com/">  
  5.             <arg0>Justin</arg0>  
  6.         </ns2:sayHello>  
  7.     </soap:Body>  
  8. </soap:Envelope>  

input:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">  
  3.     <S:Body>  
  4.         <ns2:sayHelloResponse xmlns:ns2="http://example.helloworld.com/">  
  5.             <return>Justin Say Hello To The World !</return>  
  6.         </ns2:sayHelloResponse>  
  7.     </S:Body>  
  8. </S:Envelope>  

   因此,可以看出webservice之所以能够做到跨语言与平台,是因为其是基于xml+http的方式传递数据。所谓xml就是常说的soap。可以看出,与一般http请求不同的地方是,传输 的内容是一个xml,这个xml即所谓的soap报文。



下面将着重于WSDL。 wsdl协议说明http://www.w3.org/TR/wsdl


结构

现在开始说说wsdl的结构以及各个元素的意义。
从下面这张图可以看出wsdl中各元素是存在嵌套的关系的


(reference:http://www.ibm.com/developerworks/cn/webservices/ws-wsdl/)
WSDL 文档将Web服务定义为服务访问点或端口的集合。在 WSDL 中,由于服务访问点和消息的抽象定义已从具体的服务部署或数据格式绑定中分离出来,因此可以对抽象定义进行再次使用:消息,指对交换数据的抽象描述;而端口类型,指操作的抽象集合。用于特定端口类型的具体协议和数据格式规范构成了可以再次使用的绑定。将Web访问地址与可再次使用的绑定相关联,可以定义一个端口,而端口的集合则定义为服务。

继续使用helloworld.wsdl这个例子。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"  
  3.     xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  4.     xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"  
  5.     name="HelloWorldService">  
  6.     <types>  
  7.         <xs:schema xmlns:tns="http://example.helloworld.com/"  
  8.             xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"  
  9.             targetNamespace="http://example.helloworld.com/">  
  10.             <xs:element name="sayHello">  
  11.                 <xs:complexType>  
  12.                     <xs:sequence>  
  13.                         <xs:element name="arg0" type="xs:string" minOccurs="0" />  
  14.                     </xs:sequence>  
  15.                 </xs:complexType>  
  16.             </xs:element>  
  17.             <xs:element name="sayHelloResponse">  
  18.                 <xs:complexType>  
  19.                     <xs:sequence>  
  20.                         <xs:element name="return" type="xs:string" minOccurs="0" />  
  21.                     </xs:sequence>  
  22.                 </xs:complexType>  
  23.             </xs:element>  
  24.         </xs:schema>  
  25.     </types>  
  26.     <message name="sayHello">  
  27.         <part name="parameters" element="tns:sayHello"></part>  
  28.     </message>  
  29.     <message name="sayHelloResponse">  
  30.         <part name="parameters" element="tns:sayHelloResponse"></part>  
  31.     </message>  
  32.     <portType name="HelloWorld">  
  33.         <operation name="sayHello">  
  34.             <input message="tns:sayHello"></input>  
  35.             <output message="tns:sayHelloResponse"></output>  
  36.         </operation>  
  37.     </portType>  
  38.     <binding name="HelloWorldPortBinding" type="tns:HelloWorld">  
  39.         <soap:binding transport="http://schemas.xmlsoap.org/soap/http"  
  40.             style="document"></soap:binding>  
  41.         <operation name="sayHello">  
  42.             <soap:operation soapAction=""></soap:operation>  
  43.             <input>  
  44.                 <soap:body use="literal"></soap:body>  
  45.             </input>  
  46.             <output>  
  47.                 <soap:body use="literal"></soap:body>  
  48.             </output>  
  49.         </operation>  
  50.     </binding>  
  51.     <service name="HelloWorldService">  
  52.         <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">  
  53.             <soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>  
  54.         </port>  
  55.     </service>  
  56. </definitions>  

根元素<definitions>

[html]  view plain  copy
  1. <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"  
  2.     xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  3.     xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"  
  4.     name="HelloWorldService">  
  5.   
  6. ......  
  7. </definitions>  

文档的根元素为<definitions>...</definitions>,所有需要用到的namespace也在其中描述。


数据类型定义<types>

<types>可以认为是数据容器,所有用于交换的数据类型都定义在其中,而且使用的是xml schema的语法。其更重要的用处是告诉服务端或客户端如何将收到的xml中元素值解析成本地语言中的数据类型。

[html]  view plain  copy
  1. <types>  
  2.        <xs:schema xmlns:tns="http://example.helloworld.com/"  
  3.             xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"  
  4.             targetNamespace="http://example.helloworld.com/">  
  5.             <xs:element name="sayHello">  
  6.                 <xs:complexType>  
  7.                     <xs:sequence>  
  8.                         <xs:element name="arg0" type="xs:string" minOccurs="0" />  
  9.                     </xs:sequence>  
  10.                 </xs:complexType>  
  11.             </xs:element>  
  12.             <xs:element name="sayHelloResponse">  
  13.                 <xs:complexType>  
  14.                     <xs:sequence>  
  15.                         <xs:element name="return" type="xs:string" minOccurs="0" />  
  16.                     </xs:sequence>  
  17.                 </xs:complexType>  
  18.             </xs:element>  
  19.         </xs:schema>  
  20.     </types>  

你也也可以将外数据类型定义在外部XSD文件中,然后在<types></types>中引用:

[html]  view plain  copy
  1. <import namespace="" location=""/>  


消息格式定义<message>

<message>元素定义了一个web服务中 所有交换的信息。它有一个或多个<part>的元素组成,这些M<part>不能嵌套,只能是并列关系。<types>中定义的数据类型将在这里被用到,表示<part>的类型。

[html]  view plain  copy
  1.        <message name="sayHello">  
  2.     <part name="parameters" element="tns:sayHello"></part>  
  3. </message>  
  4. <message name="sayHelloResponse">  
  5.     <part name="parameters" element="tns:sayHelloResponse"></part>  
  6. </message>  

服务中可用操作的集合<portType>

[html]  view plain  copy
  1. <portType name="HelloWorld">  
  2.     <operation name="sayHello">  
  3.         <input message="tns:sayHello"></input>  
  4.         <output message="tns:sayHelloResponse"></output>  
  5.     </operation>  
  6. </portType>  
<portType>定义了一个操作集合,它由一个或多个<operation>组成。我们可以把它理解成定义了一个函数库或类,其中的<operation>就是具体函数或类方法。那么其中的<input>与<output>就是方法的参数与返回值。参数和返回值得类型引用了上面<mesage>定义的消息格式。

协议绑定<binding>

[html]  view plain  copy
  1. <binding name="HelloWorldPortBinding" type="tns:HelloWorld">  
  2.         <soap:binding transport="http://schemas.xmlsoap.org/soap/http"  
  3.             style="document"></soap:binding>  
  4.         <operation name="sayHello">  
  5.             <soap:operation soapAction=""></soap:operation>  
  6.             <input>  
  7.                 <soap:body use="literal"></soap:body>  
  8.             </input>  
  9.             <output>  
  10.                 <soap:body use="literal"></soap:body>  
  11.             </output>  
  12.         </operation>  
  13.     </binding>  
在<binding>中,之前定义的>portType>以及其中的<opeation>将会被绑定到指定的传输协议中,即调用表明该<portyTtpe>中某个<opeation>需使用的传输协议,
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" 表明使用http/soap作为传输协议。


服务集合<service>

[html]  view plain  copy
  1. <service name="HelloWorldService">  
  2.     <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">  
  3.         <soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>  
  4.     </port>  
  5. </service>  


这部分是 具体的Web服务的定义,在这个名为HelloWorldService的Web服务中,包含了一个<port>即服务访问入口,访问地址是"http://localhost:8080/ws/helloWorld", 使用的消息模式是由前面的binding所定义的。当然也可以定义多个使用不同协议的<port>。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaopengsun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值