`

webservice之WSDL文件详解

 
阅读更多

 

 WSDL:  Web Services Description Language  网页服务描述语言

 

详解WSDL文档元素:

文件是以XML格式定义的,内容分成了5大元素:

1、<types>:webservice使用的数据类型,它是独立以机器和语言的类型定义,这些数据类型被<message>标签所使用。

2、<message>:webservice使用的消息,它定义了webservice函数的参数。在webservice中输入参数和输出参数需要分开定义,使用不同的message标签体。message定义的输入输出参数,被<portType>标签使用。

3、<portType>:webservice执行操作。该标签引用<message>标签定义的函数来描述函数的签名(操作名、输入参数、输出参数)

4、<binding>:webservice通信协议。在portType中定义的每一项操作,都在此绑定中实现。

5、<service>:该标签定义每一绑定的端口地址。

 

WSDL文档可以分为两部分。顶部分由抽象定义组成,而底部分则由具体描述组成。抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随机器或语言而变的元素。这就定义了一系列服务,截然不同的应用都可以实现。具体部分,如数据的序列化则归入底部分,因为它包含具体的定义。在上述的文档元素中,<types>、<message>、<portType>属于抽象定义层,<binding>、<service>属于具体定义层。所有的抽象可以是单独存在于别的文件中,也可以从主文档中导入。

 

 

WSDL文档一个实例:

 

  <?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://servicelifecycle.sample"
	xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
	xmlns:ax21="http://bean.servicelifecycle.sample/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://servicelifecycle.sample">
	<wsdl:documentation>Library</wsdl:documentation>
	<wsdl:types>
		<xs:schema xmlns:ax22="http://bean.servicelifecycle.sample/xsd"
			attributeFormDefault="qualified" elementFormDefault="qualified"
			targetNamespace="http://servicelifecycle.sample">
			<xs:import namespace="http://bean.servicelifecycle.sample/xsd" />
			<xs:element name="returnBook">
				<xs:complexType>
					<xs:sequence>
						<xs:element minOccurs="0" name="isbn" nillable="true"
							type="xs:string" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:complexType name="Exception">
				<xs:sequence>
					<xs:element minOccurs="0" name="Exception" nillable="true"
						type="xs:anyType" />
				</xs:sequence>
			</xs:complexType>
			<xs:element name="register">
				<xs:complexType>
					<xs:sequence>
						<xs:element minOccurs="0" name="userName" nillable="true"
							type="xs:string" />
						<xs:element minOccurs="0" name="passWord" nillable="true"
							type="xs:string" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="registerResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element minOccurs="0" name="return" type="xs:boolean" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="login">
				<xs:complexType>
					<xs:sequence>
						<xs:element minOccurs="0" name="userName" nillable="true"
							type="xs:string" />
						<xs:element minOccurs="0" name="passWord" nillable="true"
							type="xs:string" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="loginResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element minOccurs="0" name="return" type="xs:boolean" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="listLendBookResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element maxOccurs="unbounded" minOccurs="0" name="return"
							nillable="true" type="ax22:Book" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="listAvailableBookResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element maxOccurs="unbounded" minOccurs="0" name="return"
							nillable="true" type="ax22:Book" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="listAllBookResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element maxOccurs="unbounded" minOccurs="0" name="return"
							nillable="true" type="ax22:Book" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="lendBook">
				<xs:complexType>
					<xs:sequence>
						<xs:element minOccurs="0" name="isbn" nillable="true"
							type="xs:string" />
						<xs:element minOccurs="0" name="userName" nillable="true"
							type="xs:string" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
			<xs:element name="lendBookResponse">
				<xs:complexType>
					<xs:sequence>
						<xs:element minOccurs="0" name="return" nillable="true"
							type="ax22:Book" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
		<xs:schema attributeFormDefault="qualified"
			elementFormDefault="qualified" targetNamespace="http://bean.servicelifecycle.sample/xsd">
			<xs:complexType name="Book">
				<xs:sequence>
					<xs:element minOccurs="0" name="author" nillable="true"
						type="xs:string" />
					<xs:element minOccurs="0" name="isbn" nillable="true"
						type="xs:string" />
					<xs:element minOccurs="0" name="title" nillable="true"
						type="xs:string" />
				</xs:sequence>
			</xs:complexType>
		</xs:schema>
	</wsdl:types>
	<wsdl:message name="loginRequest">
		<wsdl:part name="parameters" element="ns:login" />
	</wsdl:message>
	<wsdl:message name="loginResponse">
		<wsdl:part name="parameters" element="ns:loginResponse" />
	</wsdl:message>
	<wsdl:message name="lendBookRequest">
		<wsdl:part name="parameters" element="ns:lendBook" />
	</wsdl:message>
	<wsdl:message name="lendBookResponse">
		<wsdl:part name="parameters" element="ns:lendBookResponse" />
	</wsdl:message>
	<wsdl:message name="listAllBookRequest" />
	<wsdl:message name="listAllBookResponse">
		<wsdl:part name="parameters" element="ns:listAllBookResponse" />
	</wsdl:message>
	<wsdl:message name="listLendBookRequest" />
	<wsdl:message name="listLendBookResponse">
		<wsdl:part name="parameters" element="ns:listLendBookResponse" />
	</wsdl:message>
	<wsdl:message name="returnBookRequest">
		<wsdl:part name="parameters" element="ns:returnBook" />
	</wsdl:message>
	<wsdl:message name="registerRequest">
		<wsdl:part name="parameters" element="ns:register" />
	</wsdl:message>
	<wsdl:message name="registerResponse">
		<wsdl:part name="parameters" element="ns:registerResponse" />
	</wsdl:message>
	<wsdl:message name="listAvailableBookRequest" />
	<wsdl:message name="listAvailableBookResponse">
		<wsdl:part name="parameters" element="ns:listAvailableBookResponse" />
	</wsdl:message>
	<wsdl:portType name="LibraryPortType">
		<wsdl:operation name="login">
			<wsdl:input message="ns:loginRequest" wsaw:Action="urn:login" />
			<wsdl:output message="ns:loginResponse" wsaw:Action="urn:loginResponse" />
		</wsdl:operation>
		<wsdl:operation name="lendBook">
			<wsdl:input message="ns:lendBookRequest" wsaw:Action="urn:lendBook" />
			<wsdl:output message="ns:lendBookResponse" wsaw:Action="urn:lendBookResponse" />
		</wsdl:operation>
		<wsdl:operation name="listAllBook">
			<wsdl:input message="ns:listAllBookRequest" wsaw:Action="urn:listAllBook" />
			<wsdl:output message="ns:listAllBookResponse"
				wsaw:Action="urn:listAllBookResponse" />
		</wsdl:operation>
		<wsdl:operation name="listLendBook">
			<wsdl:input message="ns:listLendBookRequest" wsaw:Action="urn:listLendBook" />
			<wsdl:output message="ns:listLendBookResponse"
				wsaw:Action="urn:listLendBookResponse" />
		</wsdl:operation>
		<wsdl:operation name="returnBook">
			<wsdl:input message="ns:returnBookRequest" wsaw:Action="urn:returnBook" />
		</wsdl:operation>
		<wsdl:operation name="register">
			<wsdl:input message="ns:registerRequest" wsaw:Action="urn:register" />
			<wsdl:output message="ns:registerResponse" wsaw:Action="urn:registerResponse" />
		</wsdl:operation>
		<wsdl:operation name="listAvailableBook">
			<wsdl:input message="ns:listAvailableBookRequest"
				wsaw:Action="urn:listAvailableBook" />
			<wsdl:output message="ns:listAvailableBookResponse"
				wsaw:Action="urn:listAvailableBookResponse" />
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="LibrarySoap11Binding" type="ns:LibraryPortType">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document" />
		<wsdl:operation name="login">
			<soap:operation soapAction="urn:login" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="lendBook">
			<soap:operation soapAction="urn:lendBook" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="register">
			<soap:operation soapAction="urn:register" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="listAllBook">
			<soap:operation soapAction="urn:listAllBook" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="returnBook">
			<soap:operation soapAction="urn:returnBook" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
		</wsdl:operation>
		<wsdl:operation name="listLendBook">
			<soap:operation soapAction="urn:listLendBook" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="listAvailableBook">
			<soap:operation soapAction="urn:listAvailableBook"
				style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:binding name="LibrarySoap12Binding" type="ns:LibraryPortType">
		<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document" />
		<wsdl:operation name="login">
			<soap12:operation soapAction="urn:login" style="document" />
			<wsdl:input>
				<soap12:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap12:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="lendBook">
			<soap12:operation soapAction="urn:lendBook" style="document" />
			<wsdl:input>
				<soap12:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap12:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="register">
			<soap12:operation soapAction="urn:register" style="document" />
			<wsdl:input>
				<soap12:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap12:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="listAllBook">
			<soap12:operation soapAction="urn:listAllBook"
				style="document" />
			<wsdl:input>
				<soap12:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap12:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="returnBook">
			<soap12:operation soapAction="urn:returnBook" style="document" />
			<wsdl:input>
				<soap12:body use="literal" />
			</wsdl:input>
		</wsdl:operation>
		<wsdl:operation name="listLendBook">
			<soap12:operation soapAction="urn:listLendBook"
				style="document" />
			<wsdl:input>
				<soap12:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap12:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="listAvailableBook">
			<soap12:operation soapAction="urn:listAvailableBook"
				style="document" />
			<wsdl:input>
				<soap12:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap12:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:binding name="LibraryHttpBinding" type="ns:LibraryPortType">
		<http:binding verb="POST" />
		<wsdl:operation name="login">
			<http:operation location="Library/login" />
			<wsdl:input>
				<mime:content type="text/xml" part="login" />
			</wsdl:input>
			<wsdl:output>
				<mime:content type="text/xml" part="login" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="lendBook">
			<http:operation location="Library/lendBook" />
			<wsdl:input>
				<mime:content type="text/xml" part="lendBook" />
			</wsdl:input>
			<wsdl:output>
				<mime:content type="text/xml" part="lendBook" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="register">
			<http:operation location="Library/register" />
			<wsdl:input>
				<mime:content type="text/xml" part="register" />
			</wsdl:input>
			<wsdl:output>
				<mime:content type="text/xml" part="register" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="listAllBook">
			<http:operation location="Library/listAllBook" />
			<wsdl:input>
				<mime:content type="text/xml" part="listAllBook" />
			</wsdl:input>
			<wsdl:output>
				<mime:content type="text/xml" part="listAllBook" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="returnBook">
			<http:operation location="Library/returnBook" />
			<wsdl:input>
				<mime:content type="text/xml" part="returnBook" />
			</wsdl:input>
		</wsdl:operation>
		<wsdl:operation name="listLendBook">
			<http:operation location="Library/listLendBook" />
			<wsdl:input>
				<mime:content type="text/xml" part="listLendBook" />
			</wsdl:input>
			<wsdl:output>
				<mime:content type="text/xml" part="listLendBook" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="listAvailableBook">
			<http:operation location="Library/listAvailableBook" />
			<wsdl:input>
				<mime:content type="text/xml" part="listAvailableBook" />
			</wsdl:input>
			<wsdl:output>
				<mime:content type="text/xml" part="listAvailableBook" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="Library">
		<wsdl:port name="LibraryHttpSoap11Endpoint" binding="ns:LibrarySoap11Binding">
			<soap:address
				location="http://localhost:8088/axis2/services/Library.LibraryHttpSoap11Endpoint/" />
		</wsdl:port>
		<wsdl:port name="LibraryHttpSoap12Endpoint" binding="ns:LibrarySoap12Binding">
			<soap12:address
				location="http://localhost:8088/axis2/services/Library.LibraryHttpSoap12Endpoint/" />
		</wsdl:port>
		<wsdl:port name="LibraryHttpEndpoint" binding="ns:LibraryHttpBinding">
			<http:address
				location="http://localhost:8088/axis2/services/Library.LibraryHttpEndpoint/" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

 

 

理解这个WSDL文件结构可能要根据写的webservice程序结构来理解:


 

 

下面标红的地方都是元素之间引用的关系。

 

1.WSDL文档的根元素:

 

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

xmlns:ns1="http://org.apache.axis2/xsd"

xmlns:ns="http://servicelifecycle.sample"

xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"

xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"

xmlns:ax21="http://bean.servicelifecycle.sample/xsd"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"

targetNamespace="http://servicelifecycle.sample">

 

 

 

<definitions>定义了文档中用到的各个xml元素的namespace缩写,也界定了本文档自己的 targetNamespace="http://servicelifecycle.sample",这意味着其它的XML要引用当前XML中的元素时,要声明这个namespace。注意xmlns:ns="http://www.jsoso.com/wstest"这个声明,它标示了使用ns这个前缀指向自身的命名空间。

 

2.WSDL文档数据类型定义元素:<types>:

 

 

<wsdl:types>

<xs:schema xmlns:ax22="http://bean.servicelifecycle.sample/xsd"

attributeFormDefault="qualified" elementFormDefault="qualified"

targetNamespace="http://servicelifecycle.sample">

<xs:import namespace="http://bean.servicelifecycle.sample/xsd" />

<xs:element name="returnBook">

<xs:complexType>

<xs:sequence>

<xs:element minOccurs="0" name="isbn" nillable="true"

type="xs:string" />

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="login"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="userName" nillable="true" type="xs:string" /> <xs:element minOccurs="0" name="passWord" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="loginResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:boolean" /> </xs:sequence> </xs:complexType> </xs:element>

 

这里定义webservice的一个元素名称returnBook,就是service的一个函数名称(见图),定义了混合类型数据类型,类型是string类型,名称叫isbn

 

3.WSDL文档消息体定义元素:< message >:

 

 

<wsdl:message name="loginRequest">

<wsdl:part name="parameters" element="ns:login" />

</wsdl:message>

<wsdl:message name="loginResponse">

<wsdl:part name="parameters" element="ns:loginResponse" />

</wsdl:message>

 

 

这里定义定义了消息请求,这里定义使用的消息是loginRequest和loginResponse,它分别引用了types中的login和loginResponse元素。在types中的login和loginResponse元素定义好了参数类型。有没有Response消息,主要是看你的函数是否有返回值,如果是void,想returnBook这函数就没有returnBookResponse。

 

4.WSDL文档操作定义元素:< portType >

 

 

<wsdl:portType name="LibraryPortType">

<wsdl:operation name="login">

<wsdl:input message="ns:loginRequest" wsaw:Action="urn:login" />

<wsdl:output message="ns:loginResponse" wsaw:Action="urn:loginResponse" />

</wsdl:operation>

 

定义了操作名称login,输入使用的消息是loginRequest,Action是urn:login,输出ns:loginResponse,Action是:urn:loginResponse

 

 

5、WSDL文档操作绑定协议<binding>:

 

<wsdl:binding name="LibrarySoap11Binding" type="ns:LibraryPortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> <wsdl:operation name="login"> <soap:operation soapAction="urn:login" style="document" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation>

 

首先<binding>标签使用<soap:binding>的transport和style属性定义了Web Service的通讯协议HTTP和SOAP的请求风格RPC。其次<operation>子标签将portType中定义的 operation同SOAP的请求绑定,定义了操作名称soapAction,输出输入参数和异常的编码方式及命名空间。 

如果客户端要调用这方法就要声明是这soapAction名称urn:login。

 

6、WSDL文档绑定服务端口地址<service>:

 

<wsdl:service name="Library"> <wsdl:port name="LibraryHttpSoap11Endpoint" binding="ns:LibrarySoap11Binding"> <soap:address location="http://localhost:8088/axis2/services/Library.LibraryHttpSoap11Endpoint/" /> </wsdl:port> <wsdl:port name="LibraryHttpSoap12Endpoint" binding="ns:LibrarySoap12Binding"> <soap12:address location="http://localhost:8088/axis2/services/Library.LibraryHttpSoap12Endpoint/" /> </wsdl:port> <wsdl:port name="LibraryHttpEndpoint" binding="ns:LibraryHttpBinding"> <http:address location="http://localhost:8088/axis2/services/Library.LibraryHttpEndpoint/" /> </wsdl:port> </wsdl:service>

 

这里描述webservice的服务名称是Libraryservice是一套<port>元素。在一一对应形式下,每个<port>元素都和一个location关联。如果同一个<binding>有多个<port>元素与之关联,可以使用额外的URL地址作为替换。 


一个WSDL文档中可以有多个<service>元素,而且多个<service>元素十分有用,其中之一就是可以根据目标URL来组织端口。在一个 WSDL文档中,<service>的name属性用来区分不同的service。在同一个service中,不同端口,使用端口的"name"属性区分。 

 

 

  • 大小: 51.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics