webservice-WSDL结构与各元素解析
关于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的例子:
soap是web service的标准通信协议,soap为simple object access protocoll的缩写,简单对象访问协议. 它是一种标准化的传输消息的XML消息格式。
调用上面hellowolrd.wsdl生成客户端后调用服务发送与接收的soap报文如下:
output:
input:
因此,可以看出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这个例子。
根元素<definitions>
文档的根元素为<definitions>...</definitions>,所有需要用到的namespace也在其中描述。
数据类型定义<types>
<types>可以认为是数据容器,所有用于交换的数据类型都定义在其中,而且使用的是xml schema的语法。其更重要的用处是告诉服务端或客户端如何将收到的xml中元素值解析成本地语言中的数据类型。
你也也可以将外数据类型定义在外部XSD文件中,然后在<types></types>中引用:
消息格式定义<message>
服务中可用操作的集合<portType>
协议绑定<binding>
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" 表明使用http/soap作为传输协议。