Webservice_03_wsdl
WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务访问点对包含面向文档信息或面向过程调用的服务进行访问(类似远程过程调用)。WSDL首先对访问的操作和访问时使用的请求/响应消息进行抽象描述,然后将其绑定到具体的传输协议和消息格式上以最终定义具体部署的服务访问点。相关的具体部署的服务访问点通过组合就成为抽象的Web服务。 本文将详细讲解WSDL文档的结构,并分析每个元素的作用。 一:WSDL定义
????WSDL是一个用于精确描述Web服务的文档,WSDL文档是一个遵循WSDL XML模式的XML文档。WSDL 文档将Web服务定义为服务访问点或端口的集合。在 WSDL 中,由于服务访问点和消息的抽象定义已从具体的服务部署或数据格式绑定中分离出来,因此可以对抽象定义进行再次使用:消息,指对交换数据的抽象描述;而端口类型,指操作的抽象集合。用于特定端口类型的具体协议和数据格式规范构成了可以再次使用的绑定。将Web访问地址与可再次使用的绑定相关联,可以定义一个端口,而端口的集合则定义为服务。 ?? 一个WSDL文档通常包含7个重要的元素,即types、import、message、portType、operation、binding、service元素。这些元素嵌套在definitions元素中,definitions是WSDL文档的根元素。文章的下一部分将会详细介绍WSDL的基本结构。 二:WSDL的基本结构--概述 如第一部分最后描述的那样,一个基本的WSDL文档包含7个重要的元素。下面将分别介绍这几个元素以及他们的作用。 WSDL 文档在Web服务的定义中使用下列元素:
可以参考下图来理解一下WSDL的文档结构图: WSDL的xml schema可以参照如下网址:http://schemas.xmlsoap.org/wsdl/ 三:WSDL的基本结构--详述 ? <types> <xsd:schema> <xsd:import namespace="http://webservice.lichen.cn/" schemaLocation="http://localhost:8888/ns?xsd=1" /> </xsd:schema> </types> 打开http://localhost:8888/ns?xsd=1
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> - <xs:schema targetNamespace="http://webservice.lichen.cn/" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.lichen.cn/"> <xs:element type="tns:add" name="add" /> <xs:element type="tns:addResponse" name="addResponse" /> <xs:element type="tns:minus" name="minus" /> <xs:element type="tns:minusResponse" name="minusResponse" /> - <xs:complexType name="minus"> - <xs:sequence> <xs:element type="xs:int" name="arg0" /> <xs:element type="xs:int" name="arg1" /> </xs:sequence> </xs:complexType> - <xs:complexType name="minusResponse"> - <xs:sequence> <xs:element type="xs:int" name="return" /> </xs:sequence> </xs:complexType> - <xs:complexType name="add"> - <xs:sequence> <xs:element type="xs:int" name="arg0" /> <xs:element type="xs:int" name="arg1" /> </xs:sequence> </xs:complexType> - <xs:complexType name="addResponse"> - <xs:sequence> <xs:element type="xs:int" name="return" /> </xs:sequence> </xs:complexType> </xs:schema>
? 1、definitions元素 <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.lichen.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.lichen.cn/" name="MyServiceImplService"> 2、types元素 <xs:complexType name="add"> - <xs:sequence> <xs:element type="xs:int" name="arg0" /> <xs:element type="xs:int" name="arg1" /> </xs:sequence> </xs:complexType> - <xs:complexType name="addResponse"> - <xs:sequence> <xs:element type="xs:int" name="return" /> </xs:sequence> </xs:complexType> 上面是数据定义部分,该部分定义了两个元素,一个是add,一个是addResponse: 3、import元素 <wsdl:import namespace="http://xxx.xx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/> ? ????必须有namespace属性和location属性: namespace属性:值必须与正导入的WSDL文档中声明的targetNamespace相匹配; location属性:必须指向一个实际的WSDL文档,并且该文档不能为空。 ? 4、message元素 message元素描述了Web服务使用消息的有效负载。message元素可以描述输出或者接受消息的有效负载;还可以描述SOAP文件头和错误detail元素的内容。定义message元素的方式取决于使用RPC样式还是文档样式的消息传递。在本文中的message元素的定义,本文档使用了采用文档样式的消息传递: ? <message name="add"> <part name="parameters" element="tns:add"></part> </message> <message name="addResponse"> <part name="parameters" element="tns:addResponse"></part> </message> ? 该部分是消息格式的抽象定义:定义了两个消息add和addResponse: ? 5、portType元素 ? <portType name="IMyService"> <operation name="add"> <input wsam:Action="http://webservice.lichen.cn/IMyService/addRequest" message="tns:add"></input> <output wsam:Action="http://webservice.lichen.cn/IMyService/addResponse" message="tns:addResponse"></output> </operation> <operation name="minus"> <input wsam:Action="http://webservice.lichen.cn/IMyService/minusRequest" message="tns:minus"></input> <output wsam:Action="http://webservice.lichen.cn/IMyService/minusResponse" message="tns:minusResponse"></output> </operation> </portType>
portType定义了服务的调用模式的类型,这里包含两个个操作add和minus方法,同时包含input和output表明该操作是一个请求/响应模式,请求消息是前面定义的minusRequest,响应消息是前面定义的minusResponse。input表示传递到Web服务的有效负载,output消息表示传递给客户的有效负载。 ? 6、binding <binding name="MyServiceImplPortBinding" type="tns:IMyService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding> <operation name="add"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal"></soap:body> </input> <output> <soap:body use="literal"></soap:body> </output> </operation> <operation name="minus"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal"></soap:body> </input> <output> <soap:body use="literal"></soap:body> </output> </operation> </binding>
这部分将服务访问点的抽象定义与SOAP HTTP绑定,描述如何通过SOAP/HTTP来访问按照前面描述的访问入口点类型部署的访问入口。其中规定了在具体SOAP调用时,应当使用的soapAction是""。 ? 7、service元素和port元素 ? <service name="MyServiceImplService"> <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding"> <soap:address location="http://localhost:8888/ns"></soap:address> </port> </service> ? 这部分是具体的Web服务的定义,在这个名为MyService的Web服务中,提供了一个服务访问入口,访问地址是http://localhost:8888/ns,使用的消息模式是由前面的binding所定义的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |