|

楼主 |
发表于 2007-8-31 14:03:49
|
显示全部楼层
有些SOAP1.1专用的绑定元素要与WSDL绑定元素组合在一起使用,这些元素包括soapbind:binding、soapbind:operation、soapbind:body、soapbind:fault、soapbind:header以及soapbind:headerfault,其中soap:binding和soapbind:body元素是必须使用的元素,而其他元素可有可无。与SOAP绑定命名空间一起使用soapbind前缀是我的习惯,但并非一定那么做,你可以使用任何的前缀,就像XML命名空间一样。但是!赋给该前缀的命名空间必须由XML1.1指定的用于SOAP1.1绑定的命名空间http://schemas.xmlsoap.org/wsdl/soap/建立关联。
【1.0 soapbind:binding元素】
Soapbind:binding元素指定了用于传输SOAP消息的internet协议以及其操作的错误消息传递样式(即RPC或文档)。让我们来看看4-1示例吧:
<binding name = “BookPrice Binding” type = “mh:BookQuote”>
<soapbind:binding style = “rpc”
Transport = http://schemas.xmlsoap.org/soap/http/>
<operation name = “getBookPrice”>
<soapbind: operation styles = “rpc”
SoapAction =
http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice/>
<input>
<soapbind.body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</input>
<output>
<soapbind:body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</output>
</operation>
</binding>
由于J2EE Web Services 只支持SOAP绑定,所以必须在WSDL binding元素中声明soapbind:binding元素。此外,必须将style属性声明为rpc或document,该属性不能为其他值。
由于HTTP是J2EE Web Services 允许的唯一传输协议,因此必须将transport属性声明成HTTP,表示它的值必须是http://schemas.xmlsoap.org/soap/http/。此传输是对应于由SOAP1.1定义的SOAP-HTTP绑定的传输。
【soapbind:operation元素】---------------------------------------------------------------------------------
soapbind:operation元素是必须使用的元素,该元素为具体的操作指定了消息传递样式(RPC或文档),并指定了SOAPAction文件头字段的值。让我们看看下面4-2示例:
<binding name = “BookPrice Binding” type = “mh:BookQuote”>
<soapbind:binding style = “rpc”
Transport = http://schemas.xmlsoap.org/soap/http/>
<operation name = “getBookPrice”>
<soapbind: operation styles = “rpc”
SoapAction =
http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice/>
<input>
<soapbind.body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</input>
<output>
<soapbind:body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</output>
</operation>
</binding>
在WSDL1。1中,style属性是可选,该属性可用于重写由soapbind:binding元素声明的默认消息传递样式-----这也是引起互操作性问题的关键,所以BP强烈要求由soapbind:operation元素声明的style属性的值要与它们的soapbind:binding元素的style属性值相同。
soapAction属性指定了在HTTP请求消息的SOAPAction文件头字段中必须放置的值。用户不需要声明WSDL soapAction 属性,即可以省略该属性。也可以将soapAction属性的值声明为空(用一队双引号表示),省略为空也就是省略了soapAction属性。
SOAPAction HTTP文件头字段的值必须与soapbind:operation元素的对应soapAction属性的值完全匹配。程序4-3给出了一个HTTP SOAP请求消息,该消息的SOAPAction文件头字段与程序1-2中声明的soapAction属性匹配。
POST led/BookQuote HTTP/1.1
Host: www.Monson-Haefel.com
Content-Type:text/html; Charset = “utf - 8”
Content-Length: nnnn
SOAPAction =
“http:// www.Monson-Haefel.com/jwsbook/BookQuote/GetbookPrice”
<?xml version = “1.0” encoding= “UTF-8”>
<sopa:Envelope
Xmlns:soap=Http://schemas.xmlsoap.org/soap/envelope/
Xmlns:mh=”Http://www.Monson-Haefel.com/jwsbook/BookQuote”>
<soap:Body>
<mh:getBookPrice>
<isbn>0930849028</isbn>
</mh:betBookPrice>
</soap:Body>
</soap:Envelope>
消息传递的样式直接影响SOAP消息体的构造方式,因此重要的是要将style声明成正确的RPC或Document。
当使用RPC样式的消息传递时,SOAP消息的Body元素包含一个表示要完成的操作的元素,该元素从定义于portType的operation元素中得到操作的名字。
Operation元素可以不包含或包含多个参数元素,这些元素派生于input消息的部分-----每个参数元素会直接映射到消息部分。
当使用文档样式的消息传递时,XML文档段是SOAP消息的Body元素的直接子元素,没有指定操作。
======================================================================
也许大家已经注意到了吧,soapbind:body元素的属性改变取决于使用RPC样式还是使用文档样式的消息传递,soapbind:body元素有4个属性,即use、namespace、part和encordingStyle属性。
Use属性的值必须是literal。如果soapbind:body元素没有声明use属性,就假设该属性的值是literal。永远不会使用encordingStyle属性,因为符合WS-I的Web服务基于W3C XML模式,而该模式隐含了use = literal声明;也不会使用其他编码样式。
Part属性指定了消息定义中正在使用的part元素,只有当用户使用由消息声明的part元素的子集时,才会使用part属性。
在RPC样式的消息中,必须用一个有效的URI指定namespace属性。此URI可以与WSDL文档的targetNamespace相同,如下4-4例:
<?xml version = “1.0” encoding= “UTF-8”>
<definitions name = “BookQuoteWS”
targetNamespace = http://www.Monson-Haefel.com/jwsbook/BookQuote
xmlns:mh=http://www.Monson-Haefel.com/jwsbook/BookQuote
xmlns:soapbind=http://schemas.xmlsoap.org/wsdl/soap/
xmlns:xsd=http://www.w3.org/2001/XMLScheam
xmlns=”http://schemas.xmlsopa.org/wsdl”>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
<!—binding 告诉了我们哪些协议和内码样式被使用-->
<binding name = “BookPrice Binding” type = “mh:BookQuote”>
<soapbind:binding style = “rpc”
Transport = http://schemas.xmlsoap.org/soap/http/>
<operation name = “getBookPrice”>
<soapbind: operation styles = “rpc”
SoapAction =
http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice/>
<input>
<soapbind.body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</input>
<output>
<soapbind:body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</output>
</operation>
</binding>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
</definitions>
与此相反,文档样式的消息不能在soapbind;body元素中指定namespace属性。XML文档的命名空间派生于它的XML文档。程序4-5给出了文档样式WSDL定义的绑定元素。
<binding name = “SubmitPurchaseOrder_Binding” type = “mh: SubmitPurchaseOrder”>
<soapbind:binding style = “document”
Transport = http://schemas.xmlsoap.org/soap/http/>
<operation name = “submit”>
<soapbind: operation styles = “document”>
<input>
<soapbind.body use = “literal”>
</input>
<output>
<soapbind:body use = “literal”>
</output>
</operation>
</binding>
======================================================================
除soapbind:body元素之外,绑定operation还可以声明fault元素。Fault消息元素与input消息元素和output消息元素处于同一级别。下面程序4-6给出了带fault和soapbind:fault元素的binding示例:
<binding name = “BookPrice Binding” type = “mh:BookQuote”>
<soapbind:binding style = “rpc”
Transport = http://schemas.xmlsoap.org/soap/http/>
<operation name = “getBookPrice”>
<soapbind: operation styles = “rpc”
SoapAction =
http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice/>
<input>
<soapbind.body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</input>
<output>
<soapbind:body use = “literal”
Namespace = http://www.Monson-Haefel.com/jwsbook/BookQuote />
</output>
<fault name = “InvalidArgumentFault”>
<soapbind:fault name = “InvalidArgumentFault” use = “literal”>
</fault>
</operation>
</binding>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
</definitions>
WSDL的fault元素和soapbind:body元素包含一个强制性的name属性,表示要引用声明于对应portType中的专用错误消息。在程序1-2中,fault元素和soapbind:fault元素表示InvalidArgumentFault,它定义于BookQuote端口和soapbind:fault元素中,如下面的4-7例:
<portType name = “BookQuote”>
<operation name = “getBookPrice”>
<input name = “isbn” message = “mh:GetBookPriceRequest”/>
<output name = “price” message = “mh:GetBookPriceRespsonse”/>
<fault name = “InvalidArgumentFault” message = “mh: InvalidArgumentFault” />
</operation>
</portType>
操作可以没有fault元素,也可以有多个fault元素,其中每个元素有其自己的soapbind:fault元素。每个soapbind:fault元素可以声明一个use属性,如果声明了一个use属性,那么值必须是literal。 |
|