微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

webservice-WSDL结构与各元素解析

承接上一遍webservice初识,这篇文章将着重于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这个例子。

<?xml version="1.0" encoding="UTF-8"?>
<deFinitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"
	name="HelloWorldService">
	<types>
		<xs:schema xmlns:tns="http://example.helloworld.com/"
			xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
			targetNamespace="http://example.helloworld.com/">
			<xs:element name="sayHello">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="arg0" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="sayHelloResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="return" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
	</types>
	<message name="sayHello">
		<part name="parameters" element="tns:sayHello"></part>
	</message>
	<message name="sayHelloResponse">
		<part name="parameters" element="tns:sayHelloResponse"></part>
	</message>
	<portType name="HelloWorld">
		<operation name="sayHello">
			<input message="tns:sayHello"></input>
			<output message="tns:sayHelloResponse"></output>
		</operation>
	</portType>
	<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document"></soap:binding>
		<operation name="sayHello">
			<soap:operation soapAction=""></soap:operation>
			<input>
				<soap:body use="literal"></soap:body>
			</input>
			<output>
				<soap:body use="literal"></soap:body>
			</output>
		</operation>
	</binding>
	<service name="HelloWorldService">
		<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
			<soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>
		</port>
	</service>
</deFinitions>

根元素<deFinitions>

<deFinitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://example.helloworld.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.helloworld.com/"
	name="HelloWorldService">

......
</deFinitions>

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


数据类型定义<types>

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

<types>
	   <xs:schema xmlns:tns="http://example.helloworld.com/"
			xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
			targetNamespace="http://example.helloworld.com/">
			<xs:element name="sayHello">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="arg0" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="sayHelloResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="return" type="xs:string" minOccurs="0" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
	</types>

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

<import namespace="" location=""/>


消息格式定义<message>

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

        <message name="sayHello">
		<part name="parameters" element="tns:sayHello"></part>
	</message>
	<message name="sayHelloResponse">
		<part name="parameters" element="tns:sayHelloResponse"></part>
	</message>

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

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

协议绑定<binding>

<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document"></soap:binding>
		<operation name="sayHello">
			<soap:operation soapAction=""></soap:operation>
			<input>
				<soap:body use="literal"></soap:body>
			</input>
			<output>
				<soap:body use="literal"></soap:body>
			</output>
		</operation>
	</binding>
在<binding>中,之前定义的>portType>以及其中的<opeation>将会被绑定到指定的传输协议中,即调用表明该<portyTtpe>中某个<opeation>需使用的传输协议,
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" 表明使用http/soap作为传输协议。


服务集合<service>

	<service name="HelloWorldService">
		<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
			<soap:address location="http://localhost:8080/ws/helloWorld"></soap:address>
		</port>
	</service>


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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐