以前接触过的一个项目是对wsdl进行语义标注,达到语义互操作的目的,现对wsdl总结如下。
WSDL(Web Services Description Language)-网络服务描述语言,是一门基于xml的语言,用于描述web services以及如何对它们进行访问。
WSDL文档的作用:
需要运行web service的请求者找到wsdl文档(介绍如何运行web服务),解析文档,基于wsdl发送一个SOAP请求到提供者,获得服务的运行。
WSDL文档结构:
<!--指出命名空间、schema等--> <definitions> <!--ws使用的数据类型--> <types> </types> <!--消息,描述消息的组成和消息中组成部分的数据类型--> <message> </message> <!--端口类型,定义ws提供的操作--> <portType> </portType> <!--指定数据格式和通信协议,一个binding对应一个端口,指出如何访问、使用ws--> <binding> </binding> <!--端口(服务)的集合,指出ws的位置--> <service> <documentation/> <!--为一个特定端口类型(操作)指定单独的访问地址--> <port> </port> </service> </definitions>
实例:
<definitions name="Terms" targetNamespace="http://example.com/terms.wsdl" xmlns:tns="http://example.com/terms.wsdl" xmlns:soap="http://schema.xmlsoap.org/wsdl"> <message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <!--函数库glossaryTerms,包括的函数有getTerm,该函数的输入参数为getTermRequest,返回为getTermResponse--> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType> </definitions>
为了更形象的理解wsdl,可与编程语言联系起来。portType可比作编程语言中的一个函数库,或者模块,或者一个类,里面包括了各种函数(操作operation)。message可比作一个函数调用的参数,如输入参数(String str,int num)和返回,它定义每个消息的部件以及部件的数据类型。
绑定binding:绑定为web service定义消息格式和通信协议。
<!--type为绑定的端口名--> <binding type="glossaryTerms" name="b1"> <soap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <operation> <soap:operation soapAction="http://example.com/getTerm" /> <input> <!--指出输入输出的编码格式--> <soap:body use="literal" /> </input> <output> <soap:body use="literal" /> </output> </operation> </binding>
服务:
<service name="termService"> <documentation></documentation> <port name="getTermsPort" binding="tns:b1"> <soap:address location="http://example.com/term"/> </port> </service>
大致了解下即可,因为不会让我们手写WSDL,发布ws时可以自动生成(如tuscany或axis2发布服务时都自动生成了wsdl文档)。