WSDL(网络服务描述语言,Web Services Description Language)是一门基于XML的语言,用于描述WebService以及如何对它们进行访问。编程
WSDL将Web服务描述定义为一组服务访问点,客户端能够经过这些服务访问点对包含面向文档信息或面向过程调用的服务进行访问。WSDL首先对访问的操做和访问时使用的请求/响应消息进行抽象描述,而后将其绑定到具体的传输协议和消息格式上以最终定义具体部署的服务访问点。网络
WSDL是一个用于精确描述Web服务的文档,WSDL文档是一个遵循WSDL XML模式的XML文档。WSDL文档将Web服务定义为服务访问点或端口的集合。在WSDL中,因为服务访问点和消息的抽象定义已从具体的服务部署或数据格式绑定中分离出来,所以能够对抽象定义进行再次使用:消息,指对交换数据的抽象描述;而端口类型,指操做的抽象集合。用于特定端口类型的具体协议和数据格式规范构成了能够再次使用的绑定。将Web访问地址与可再次使用的绑定相关联,能够定义一个端口,而端口的集合则定义为服务。数据结构
WSDL文档一般包含7个重要的元素,即types、import、message、portType、operation、binding、service元素。全部元素嵌套在definitions元素中,definitions是WSDL文档的根元素。ide
Types - 数据类型定义的容器,使用某种类型系统(通常地使用XML Schema中的类型系统)。 模块化
Message - 通讯消息的数据结构的抽象类型化定义。使用Types所定义的类型来定义整个消息的数据结构。 函数
Operation - 对服务中所支持的操做的抽象描述,通常单个Operation描述了一个访问入口的请求/响应消息对。 编码
PortType - 对于某个访问入口点类型所支持的操做的抽象集合,这些操做能够由一个或多个服务访问点来支持。 spa
Binding - 特定端口类型的具体协议和数据格式规范的绑定。 orm
Port - 定义为协议/数据格式绑定与具体Web访问地址组合的单个服务访问点。 xml
Service- 相关服务访问点的集合。
WSDL文档主要结构以下:
<definitions> <types> definition of types........</types> <message> definition of a message....</message> <portType> definition of a port.......</portType> <binding> definition of a binding....</binding> </definitions>
WSDL文档实例:
<message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType>
<portType>元素把"glossaryTerms"定义为某个端口的名称,把 "getTerm"定义为某个操做的名称。
操做"getTerm"拥有一个名为"getTermRequest"的输入消息,以及一个名为"getTermResponse"的输出消息。
<message>元素可定义每一个消息的部件,以及相关联的数据类型。
对比传统的编程,glossaryTerms是一个函数库,而"getTerm"是带有输入参数"getTermRequest"和返回参数getTermResponse的一个函数。
WSDL文档的阅读顺序从下往上读。
每一个WSDL有且只有一个Service节点。
A、先找Service节点
B、Service节点中找port节点。每一个port对应一个PortType。
C、Port节点对应一binding节点。每一个binding节点对应一个PortType
D、PortType中有operation 节点就是服务的方法。
E、operation 中有Input(参数)和output(返回值)
F、Input(参数)和output(返回值)对应message节点
G、Message对应element节点。Element节点对应complexType节点描述了参数及返回值的数据类型。
简单的Web Service的WSDL文档以下:
<?xml version="1.0" encoding="UTF-8" ?> <wsdl:definitions targetNamespace="http://com.liuxiang.xfireDemo/HelloService" xmlns:tns="http://com.liuxiang.xfireDemo/HelloService" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified targetNamespace="http://com.liuxiang.xfireDemo/HelloService"> <xsd:element name="sayHello"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="name" nillable="true" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="sayHelloResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="tns:sayHelloResponse" /> </wsdl:message> <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="tns:sayHello" /> </wsdl:message> <wsdl:portType name="HelloServicePortType"> <wsdl:operation name="sayHello"> <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" /> <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloServiceHttpBinding" type="tns:HelloServicePortType"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="sayHello"> <wsdlsoap:operation soapAction="" /> <wsdl:input name="sayHelloRequest"> <wsdlsoap:body use="literal" /> </wsdl:input> <wsdl:output name="sayHelloResponse"> <wsdlsoap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloService"> <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding"> <wsdlsoap:address location="http://localhost:8080/services/HelloService" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
服务支持名为sayHello的惟一操做,sayHello操做经过在http上运行SOAP协议来实现的。请求接受一个字符串name,通过处理后返回一个简单的字符串。
全部的WSDL文档的根元素均是definitions元素。definitions元素封装了整个文档,同时经过其name提供了一个WSDL文档。除了提供一个命名空间外,definitions元素没有其余做用。
<wsdl:definitions targetNamespace="http://com.liuxiang.xfireDemo/HelloService" xmlns:tns="http://com.liuxiang.xfireDemo/HelloService" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> </wsdl:definitions>
WSDL采用了W3C XML模式内置类型做为其基本类型系统。types元素用做一个容器,用于定义XML模式内置类型中没有描述的各类数据类型。当声明消息部分的有效负载时,消息定义使用了在types元素中定义的数据类型和元素。在本文的WSDL文档中的types定义:
<wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://com.liuxiang.xfireDemo/HelloService"> <xsd:element name="sayHello"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="name" nillable="true" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="sayHelloResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types>
数据定义部分定义了两个元素,一个是sayHello,一个是sayHelloResponse:
sayHello:定义了一个复杂类型,仅仅包含一个简单的字符串,未来用来描述操做的参入传入部分;
sayHelloResponse:定义了一个复杂类型,仅仅包含一个简单的字符串,未来用来描述操做的返回值;
import元素使得能够在当前的WSDL文档中使用其余WSDL文档中指定的命名空间中的定义元素。本例中没有使用import元素。一般在用户但愿模块化WSDL文档的时候,import功能是很是有效果的。
import的格式以下:
<wsdl:import namespace="http://xxx.xxx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/>
必须有namespace属性和location属性:
namespace属性:值必须与正导入的WSDL文档中声明的targetNamespace相匹配;
location属性:必须指向一个实际的WSDL文档,而且该文档不能为空。
message元素描述了Web服务使用消息的有效负载。message元素能够描述输出或者接受消息的有效负载;还能够描述SOAP文件头和错误detail元素的内容。定义message元素的方式取决于使用RPC样式仍是文档样式的消息传递。在本文中的message元素的定义,本文档使用了采用文档样式的消息传递:
<wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="tns:sayHelloResponse" /> </wsdl:message> <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="tns:sayHello" /> </wsdl:message>
消息格式的抽象定义:定义了两个消息sayHelloResponse和sayHelloRequest:
sayHelloRequest:sayHello操做的请求消息格式,由一个消息片段组成,名字为parameters,元素是咱们前面定义的types中的元素;
sayHelloResponse:sayHello操做的响应消息格式,由一个消息片段组成,名字为parameters,元素是咱们前面定义的types中的元素;
若是采用RPC样式的消息传递,只须要将文档中的element元素应以修改成type便可。
portType元素定义了Web服务的抽象接口。该接口有点相似Java的接口,都是定义了一个抽象类型和方法,没有定义实现。在WSDL中,portType元素是由binding和service元素来实现的,这两个元素用来讲明Web服务实现使用的Internet协议、编码方案以及Internet地址。
一个portType中能够定义多个operation,一个operation能够看做是一个方法,本文中WSDL文档的定义:
<wsdl:portType name="HelloServicePortType"> <wsdl:operation name="sayHello"> <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" /> <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" /> </wsdl:operation> </wsdl:portType>
portType定义了服务的调用模式的类型,包含一个操做sayHello方法,同时包含input和output代表该操做是一个请求/响应模式,请求消息是前面定义的sayHelloRequest,响应消息是前面定义的sayHelloResponse。input表示传递到Web服务的有效负载,output消息表示传递给客户的有效负载。
binding元素将一个抽象portType映射到一组具体协议(SOAO和HTTP)、消息传递样式、编码样式。一般binding元素与协议专有的元素和在一块儿使用,本文中的例子:
<wsdl:binding name="HelloServiceHttpBinding" type="tns:HelloServicePortType"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="sayHello"> <wsdlsoap:operation soapAction="" /> <wsdl:input name="sayHelloRequest"> <wsdlsoap:body use="literal" /> </wsdl:input> <wsdl:output name="sayHelloResponse"> <wsdlsoap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding>
binding元素将服务访问点的抽象定义与SOAP HTTP绑定,描述如何经过SOAP/HTTP来访问按照前面描述的访问入口点类型部署的访问入口。其中规定了在具体SOAP调用时,应当使用的soapAction是""。
service元素包含一个或者多个port元素,其中每一个port元素表示一个不一样的Web服务。port元素将URL赋给一个特定的binding,甚至可使两个或者多个port元素将不一样的URL赋值给相同的binding。文档中的例子:
<wsdl:service name="HelloService"> <wsdl:port name="HelloServiceHttpPort" binding="tns:HelloServiceHttpBinding"> <wsdlsoap:address location="http://localhost:8080/services/HelloService" /> </wsdl:port> </wsdl:service>
这部分是具体的Web服务的定义,在这个名为HelloService的Web服务中,提供了一个服务访问入口,访问地址是http://localhost:8080/xfire/services/HelloService,使用的消息模式是由前面的binding所定义的。