知道美河 | 上传资料 | VIP申请 | 精品课程 | 资料搜索 | 问题反馈 | 会员手册 | 积分消费 | 积分充值 | 帐号保护
美河学习学习在线赞助VIP

美河学习在线(主站) eimhe.com

 找回密码
 建立账号
查看: 10282|回复: 7

[原创] SOAP绑定(结束)

[复制链接]
发表于 2007-8-31 14:01:26 | 显示全部楼层 |阅读模式
原本想加在发布的WSDL文档后面的。。可是看了看 只占了一楼 根本不够
索性 在发张帖子吧。。。。
我想。。现在市面上能有这么详细的 J2EE Web Services的很少吧。。(请您务必现看我发布的SOAP文档 然后是WSDL文档  最后才是这篇。。。一共三篇帖子都是单独的)


好了。。 J2EE Web Services 的核心内容经过差不多一个月的时间我完全写完了。。写的不好请大家见谅
至于UDDI在Web Services里不是必须的~~~好累。。。休息一会 抽烟去咯。。

如果你需要这些 请尊重我的劳动成果 请务随意转载 如需:请注明作者。。。   MSN:xt19833@56.com
最后啰嗦一句~~大家喜欢 可以关注我的博客http://i.cn.yahoo.com/xt19833
大家一起进步。。。

[ 本帖最后由 xt19833 于 2007-8-31 14:11 编辑 ]
 楼主| 发表于 2007-8-31 14:03:49 | 显示全部楼层
SOAP 绑定

       有些SOAP1.1专用的绑定元素要与WSDL绑定元素组合在一起使用,这些元素包括soapbind:bindingsoapbind:operationsoapbind:bodysoapbind:faultsoapbind:header以及soapbind:headerfault,其中soap:bindingsoapbind: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属性声明为rpcdocument,该属性不能为其他值。
       由于HTTPJ2EE 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>

       WSDL11中,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声明成正确的RPCDocument
       当使用RPC样式的消息传递时,SOAP消息的Body元素包含一个表示要完成的操作的元素,该元素从定义于portTypeoperation元素中得到操作的名字。

Operation
元素可以不包含或包含多个参数元素,这些元素派生于input消息的部分-----每个参数元素会直接映射到消息部分。

       当使用文档样式的消息传递时,XML文档段是SOAP消息的Body元素的直接子元素,没有指定操作。

Soapbind:body 元素
======================================================================
       也许大家已经注意到了吧,soapbind:body元素的属性改变取决于使用RPC样式还是使用文档样式的消息传递,soapbind:body元素有4个属性,即usenamespacepartencordingStyle属性。

Use
属性的值必须是literal。如果soapbind:body元素没有声明use属性,就假设该属性的值是literal。永远不会使用encordingStyle属性,因为符合WS-IWeb服务基于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:fault元素
======================================================================
       soapbind:body元素之外,绑定operation还可以声明fault元素。Fault消息元素与input消息元素和output消息元素处于同一级别。下面程序4-6给出了带faultsoapbind: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
 楼主| 发表于 2007-8-31 14:06:24 | 显示全部楼层
Soapbind:header
======================================================================
       在前面的SOAP文档中我们介绍过SOAP消息可以有文件头。WSDL在绑定的input、它的output元素 ,或在两者中利用soapbind:header元素显式指定了一个SOAP文件头。下面4-8例子创建了一个message-id文件头的绑定,如下:

<types>



<xsd:schema

targetNamespace = “http://www.Monson-Haefel.com/jwsbook/BookQuote”>
<xsd:element name = message-id type = string/ >
</xsd:schema>


</types>

<message name = “Header”>

<part name = “message-id” element = “mh:message-id”/>

</message>

<message name = “GetBookPriceRequest”>

<part name = “isbn” type = “xsd:string”>

</messgae>

<message name = “GetBookPriceResponse”>

<part name = “price” type = “xsd:float”>

</messgae>

<!—portType 元素描述了web services 的抽象接口-->
<portType name = “BookQuote”>

<operation name = “getBookPrice”>


<input name = “isbn” message = “mh:GetBookPriceRequest”/>


<output name = “price” message = “mh:GetBookPriceRespsonse”/>


</operation>

</portType>

<!—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”

Soap:Action =
http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice/>
<input>

<soapbind.header use = “literal” message = “mh:Header”

part = “message-id”/>


<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>

       此例中,由soapbind:header 元素使用的message属性和part属性引用了用于错误的专用消息part。所引用的part属性必须使用一个element属性。
       也就是说,用户不能将文件头基于一个类型定义,它必须基于types元素中定义的顶级元素,或基于在其他XML模式文档或在WSDL文档中导入的顶级元素。

Use
属性的值总会是literal,与是否显示声明无关。


Soapbind:headerfault元素
======================================================================

Soapbind:headerfault
元素描述了文件头专用的错误消息,在前面的SOAP文档说过,如果有一个响应消息,那么必须在该消息的Header元素中返回各种文件头专用错误,WSDL通过要求将
Soapbind:headerfault
元素嵌套于它们关联的文件头中来保持这种要求。比如,我们可以按程序4-9,如下:

<types>

<xsd:schema

targetNamespace = “http://www.Monson-Haefel.com/jwsbook/BookQuote”>
<xsd:element name = message-id type = string/ >
</xsd:schema>

</types>


<message name = “Header”>

<part name = “message-id” element = “mh:message-id”/>

</message>

<message name = “HeadeFault”>

<part name = “HeadeFault” element = “mh:detailMessage” />

</message>


<message name = “GetBookPriceRequest”>

<part name = “isbn” type = “xsd:string”>

</messgae>

<message name = “GetBookPriceResponse”>

<part name = “price” type = “xsd:float”>

</messgae>

<!—portType 元素描述了web services 的抽象接口-->
<portType name = “BookQuote”>

<operation name = “getBookPrice”>


<input name = “isbn” message = “mh:GetBookPriceRequest”/>


<output name = “price” message = “mh:GetBookPriceRespsonse”/>


</operation>

</portType>

<!—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”

Soap:Action =
http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice/>
<input>

<soapbind.header use = “literal” message = “mh:Header”

part = “message-id”>

<soapbind:headerfault message = “mh:Header” use = “literal” />

</soapbind.header>

<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>

       soapbind:headerfault元素的要求与对soapbind:header元素的要求相同,即它必须声明一个指向对应message定义的message属性,而且不论是显示声明use属性还是使用它的默认值,use属性的值必须等于literal

WSDL实现:services元素和port元素
======================================================================

Services
元素包含了一个或多个port元素,其中每个port元素表示一个不同的Web服务。Port元素将URL赋给了一个特定binding。请看下面程序5-1针对程序1-2参考:


<services name = “BookPriceServices”>


<port name = “BookPrice_port” binding = “mh:BookPrice_Binding”>


<soapbind:address location =

http://www.Monson-Haefel.com/jwsbook/BookQuote />

</port>

</services>

Services
元素可以有多个port元素,且每个port元素将一个URL赋给一个特定绑定,甚至使两个或多个port元素将不同的URL赋给相同的binding,当进行负载平衡与容错时,也许需要这样做。下次程序5-2给出了一个包含三个port元素,且其中两个port元素指向了相同bindingservices元素。

<services name = “BookPriceServices”>

<port name = “BookPrice_port” binding = “mh:BookPrice_Binding”>


<soapbind:address location =

http://www.Monson-Haefel.com/jwsbook/BookQuote />

</port>


<port name = “BookPrice_Failover_port” binding = “mh:BookPrice_Binding”>

<soapbind:address location =

http://www.Monson-Haefel.com/jwsbook/BookPrice />

</port>


<port name=“SubmitPurchaseOrder_Port” binding = “mh: SubmitPurchaseOrder _Binding”>

<soapbind:address location =

http://www.Monson-Haefel.com/jwsbook/PO />

</port>


</services>

Soapbind:address元素

       此元素非常简单,它只是将internet地址通过其location属性赋予一个SOAP绑定。值得注意的是:相同的WSDL文档中的两个或多个port元素不能对soapbind:address元素的location属性指定相同的值。。。
发表于 2007-9-1 17:03:09 | 显示全部楼层
发表于 2007-9-1 19:03:58 | 显示全部楼层

是原创哦
支持
不知道我什么时候也能整个原创呢
真羡慕
 楼主| 发表于 2007-9-2 10:01:37 | 显示全部楼层
如果要弄成视频 等于是把WEB SERVICES + 背后的知识一起讲了。。比如:SOAP与附件JAVA API(SAAJ),JAVA API FOR WSDL(JWSDL),
JAVA API FOR XML-based RPC(JAX-RPC) 等等 许多背景知识 太长了
这个文档只是个协议的入门。。 因为美河关于J2EE WEB SERVICES的很少。。所以我就把自己写的发上来了
 楼主| 发表于 2008-1-20 15:51:29 | 显示全部楼层
发表于 2009-4-6 08:41:55 | 显示全部楼层
您需要登录后才可以回帖 登录 | 建立账号

本版积分规则

 
QQ在线咨询

QQ|小黑屋|手机版|Archiver|美河学习在线 ( 浙网备33020302000026号 )

GMT+8, 2025-5-2 07:25

Powered by Discuz!

© 2001-2025 eimhe.com.

快速回复 返回顶部 返回列表