|

楼主 |
发表于 2007-8-29 10:28:03
|
显示全部楼层
【用于RPC样式Web服务的message元素】------------------------------------------------------
当使用RPC样式的消息传递时,message元素描述了SOAP请求和应答消息的有效负载。这些元素可以描述调用参数,调用返回值,文件头以及错误。比如,SOAP文档中的BookQuote Web服务用2个message元素来描述该服务的参数与返回值(以后在介绍如何用消息描述错误和文件头)下面1-7例子:
<message name = “GetBookPriceRequest”>
<part name = “isnb” type = “xsd:string”>
</messgae>
<message name = “GetBookPriceResponse”>
<part name = “price” type = “xsd:float”>
</messgae>
GetBookPriceRequest消息表示参数(即输入),GetBookPriceResponse表示应答(即输出)。换句话说,GetBookPriceRequest消息藐视了从SOAP客户向BookQuote Web服务所传递的消息的有效负载,而GetBookPriceResponse消息描述了由BookQuote Web服务传递回客户的有效负载。
其实命名消息并没有规定习惯,在我这次写的文档中,从SOAP客户传递给服务器的消息附加有后缀Request,而传递回客户的消息则附加后缀Response。大家可以采用自己的命名习惯,就是说消息名称可以是任何名称,它不过是用来标识消息定义的而已。消息元素不能将其本身声明为输入或输出(我想在operation元素中在具体加以区分吧),因此消息命名成request或response并不能确定如何使用这些消息。
在RPC样式的消息中,消息通常分为多个部分。比如,可以用多个part元素定义一个名为GetBookPriceRequest的消息,其中每个元素表示不同的参数,如下 1-8例:
<message name = “GetBookPriceRequest”>
<part name = “isnb” type = “xsd:string”>
<part name = “quantity” type = “xsd:int” >
</messgae>
<message name = “GetBookPriceResponse”>
<part name = “price” type = “xsd:float”>
</messgae>
Web服务中的输入和输出均可以有多个部分,这一点与Java方法调用语义不同,后者允许多个参数输入,但只有一个输出(看成返回值)。然而,对于像C++,C#和Perl等这样的编程语言来说,更常见的情况是它们声明的参数即可以是输入参数,也可以是输出参数。
Wsdl是一个中性编程语言,所以它必须有足够的灵活性,从而与所有编程语言相兼容,而不是为了某个语言。SAAJ和JAX-RPC均具有用多个部分支持输出消息的功能。
【文档样式Web服务的message元素】------------------------------------------------------------------
当用户采用文档样式消息传递模式时,message元素定义要引用types定义中的顶级元素。如下例1-9:描述一个SubmitPurchasePrder Web Services的WSDL文档,PO命名空间是以前SOAP文档中的。此例是单向消息传递模式,所以没有应答,这在文档样式中是最常见的了。
<definitions name = “SubmitPurchasePrder”
targetNamespace = http://www.Monson-Haefel.com/jwsbook/PO
xmlns:mh=http://www.Monson-Haefel.com/jwsbook/PO
xmlns:soapbind=http://schemas.xmlsoap.org/wsdl/soap/
xmlns:xsd=http://www.w3.org/2001/XMLScheam
xmlns=”http://schemas.xmlsopa.org/wsdl”>
<types>
<xsd:schema
targetNamespace = “http://www.Monson-Haefel.com/jwsbook/PO”>
<!—导入这个SubmitPurchasePrder XML 规则文档-->
<xsd:import namespace = http://www.Monson-Haefel.com/jwsbook/PO
schemaLocation = http://www.Monson-Haefel.com/jwsbook/po.xsd />
</xsd:schema>
</types>
<message name = “SubmitPurchasePrderMessage”>
<part name = “isnb” element = “mh: purchasePrder”>
</messgae>
……………………………………………..
</definitions>
消息部分可以声明type属性或者element属性,但不能同时声明两者。具体声明哪一种属性取决于传递消息的种类。如果用户使用的是RPC样式的消息传递,那么part元素必须使用type属性:如果用户使用的是文档样式的消息传递,part元素则必须是element属性。RPC样式的消息传递用类型来定义过程调用,该调用中每个元素表示某一类型的参数。此外,文档样式的消息传递要交换XML文档段,并引用它们的顶级元素(即全局元素)。
【声明错误消息】-----------------------------------------------------------------------------------------------
用户可以用消息定义声明错误,其声明方式与用消息等译声明输入和输出消息的方式相同。比如下面程序2-1定义了一个错误消息,如果BookQuote请求消息包含了一个无效的ISBN,则会将该错误消息返回。它就包含一个部分,就是错误消息。
<definitions name = “BookQuote”
targetNamespace = http://www.Monson-Haefel.com/jwsbook/PO
xmlns:mh=http://www.Monson-Haefel.com/jwsbook/PO
xmlns:soapbind=http://schemas.xmlsoap.org/wsdl/soap/
xmlns:xsd=http://www.w3.org/2001/XMLScheam
xmlns=”http://schemas.xmlsopa.org/wsdl”>
<types>
<xsd:schema
<xsd:element name = “InvalidIsbnFaultDetail”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name = “offending-value” type = “xsd:string” />
<xsd:element name = “conformance-rules” type = “xsd:string” />
</xsd:sequence>
</xsd:complexType>
<xsd:element>
</xsd:schema>
</types>
<message name = “SubmitPurchasePrderMessage”>
<part name = “isnb” element = “mh: purchasePrder”>
</messgae>
<message name = “InvalidArgumentFault”>
<part name = “error_message” element = “mh: InvalidArgumentFault” />
</messgae>
……………………………………………..
</definitions>
【portType元素】-----------------------------------极为重点-----------------------------------------------
portType元素定义了Web服务的抽象接口。从概念上讲,该接口有点像java接口,因为它定义了一个抽象类型和它的方法,但没有定义实现。在WSDL中,portType元素是由binding元素和services元素实现的,这两个元素用于说明Web服务实现使用的Internet协议,编码方案以及Internet地址。
portType元素的“方法”是它的operation元素。portType元素可以有一个或多个operation,其中每个operation元素定义了一个RPC样式或文档样式Web服务方法。每个operation元素最多是由一个input或output元素以及任意数量的fault元素组成。
在下面的2-2例子中名为“BookQuote”的portType元素(左列)声明了三个操作,即GetBookPrice,GetBulkBookPrice和GetBookIsbn。右为对应的Java接口。如下例:
WSDL portType定义
| | <portType name = “BookQuote”>
<operation name = “GetBeokPrice”>
<input name = “isbn”
| Public interface BookQuote{
Public float getBookPrice
(String isbn);
| Message = “mh:GetBookPriceRequest”/>
<output name = “price”
Message = “mh:GetBookPriceResponse” />
</operation>
<!----------------------------------------------->
<operation name = “GetBulkBoekPrice”>
<input name = “request”
Message = “mh: GetBulkBoekPrice”/>
<output name = “price”
Message = “mh: GetBulkBoekPrice” />
</operation>
<!----------------------------------------------->
<operation name = “GetBookIsbn”>
<input name = “title”
Message = “mh: GetBookIsbn Request”/>
<output name = “price”
Message = “mh: GetBookIsbn Response” />
</operation>
|
Public float getBulkBoekPrice
(String isbn,int quantity);
Public String getBookIsbn(String bookTitle);
| WSDL,portType与java接口并不完全相同,但它们非常接近。其实,大多数给予java的Web服务代码生成器均在java接口和WSDL portType元素之间创建了映射。它们其实是根据WSDL的portType元素生成java接口,而且也可以根据简单的Java接口生成portType元素,operation元素和message元素。
WSDL文档可以有一个或多个portType元素,其中每个portType元素描述了不同Web服务的抽象接口,比如:一个WSDL Web服务可以定义一个名为BookQuote的PortType元素和一个名为SubmitpurchaseOrder的portType元素。 |
|