精彩的人生

好好工作,好好生活

BlogJava 首页 新随笔 联系 聚合 管理
  147 Posts :: 0 Stories :: 250 Comments :: 0 Trackbacks
在本文寫作時,並無任何 SOAP 實作,能進行作業名稱的多載動作。這對使用 Java 的客戶端是很重要的,因為使用 Java 的伺服端所使用的介面,會利用 Java 的多載功能。但這對使用 COM 的客戶端倒無所謂,因為 COM 並不支援多載。

<binding> 與 <operation> 元素
Binding 區段是,指定通訊協定、序列化、與編碼的地方。若 Types、Messages、與 PortType 等區段負責抽象的資料內容,那麼 Binding 區段便負責處理資料傳輸的具體細節。Binding 區段負責使前三個區段的抽象具體化。

將繫結規格從資料與訊息宣告中分離出來的意義是,執行相同業務的服務提供者,可將一組作業 (portType) 標準化。若欲凸顯自身的差異,每個提供者可另外提供自訂繫結。讓 WSDL 擁有匯入 construct 是有好處的;因為如此一來,即可遠離 Bindings 與 Services 區段,將抽象定義置於自身的檔案中;並可將其散佈於服務提供者間,讓他們把抽象定義設定成標準。例如,銀行可標準化,一組抽象 WSDL 文件詳盡說明的銀行作業。但每家銀行還是可以自由地,「自訂」基礎通訊協定、序列最佳化、與編碼。

下列為 Binding 區段的 WSDL 多載範例,於此重複以方便詳細討論:

<binding name="fooSampleBinding" type="wsdlns:fooSamplePortType">
<stk:binding preferredEncoding="UTF-8" />
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="foo">
<soap:operation soapAction="http://tempuri.org/action/foo1"/>
<input name="foo1">
<soap:body use="encoded"
namespace="http://tempuri.org/message/"
encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/" />
</input>
</operation>
<operation name="foo">
<soap:operation soapAction="http://tempuri.org/action/foo2"/>
<input name="foo2">
<soap:body use="encoded"
namespace="http://tempuri.org/message/"
encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/" />
</input>
</operation>
</binding>

<binding> 元素會有個名稱 (本例中為「fooSampleBinding」),以方便 Services 區段的 <port> 元素參照。它有個參照 <portType> 的「type」屬性,在本例中為「wsdlns:fooSamplePortType」。第二行是 MSTK2 擴充元素 <stk:binding>,它可指定 preferredEncoding,或稱「UTF-8」。

<soap:binding> 元素可指定,使用的樣式 (rpc) 與傳輸。傳輸屬性會參照命名空間,以確定所使用的 HTTP SOAP 通訊協定。

有兩個 <operation> 元素具有相同的名稱「foo」。這兩個作業不同之處在於,兩個不同的 <input> 名稱:「foo1」與「foo2」。在兩個 <operation> 元素中,<soap:operation> 元素的「soapAction」屬性皆相同,都是一個 URI。soapAction 屬性是 SOAP 專屬的 URI,並逐字依 SOAP 訊息使用。以此方式取得的 SOAP 訊息,會有個 SOAPAction 標頭;而且還會以 <soap:operation> 元素中的 URI 為其值。雖然 HTTP 繫結需要 soapAction 屬性,但非 HTTP 繫結則不用。本文刊出時,其用途仍然不明。就本範例而言,它似乎可用來區別兩個不同的「foo」作業。SOAP 1.1 宣稱,soapAction 可用來識別訊息的「目的 (intent)」。它更建議伺服端,可用此屬性路由訊息,而不用剖析整個訊息。實務上,其用途差異很大。<soap:operation> 元素也可以含另一個名為「style」的屬性;若特定作業需要覆寫 <soap:binding> 元素中所指定的樣式,則可使用此屬性。

<operation> 元素所含的 <input>、<output>、與 <fault> 元素,可對應 PortTypes 區段的相同元素。在上例中出現的只有 <input> 元素。在本例中,這三個元素都各有一個選擇性的「name」屬性,可用以區別名稱相同的作業。在範例的 <input> 元素中,有個 <soap:body> 元素,它可指定輸入結果 SOAP 訊息之 <body> 的內容。此元素有下列屬性:

Use
它是用以指定資料採「編碼 (encoded)」或「常值 (literal)」。「常值」的意義是,結果 SOAP 訊息所含資料的格式,完全依照抽象定義 (Types、Messages、與 PortTypes 區段) 的指定規格 。「編碼」的意義是,「encodingStyle」屬性 (參考下文) 會指定編碼方式。
Namespace
每個 SOAP 訊息的主體,都有自己的命名空間,以防止名稱衝突。此屬性所指定的 URI,會逐字使用於結果 SOAP 訊息之中。
EncodingStyle
以 SOAP 的編碼方式而言,URI 的值應該是「http://schemas.xmlsoap.org/soap/encoding」。
文件樣式繫結
在上一區段中,<soap:binding> 元素有個類型屬性被設定成「rpc」。若將此屬性設定成「document」,即可改變訊息在線上的排序。此時這些訊息便成了文件傳輸,而不再是函數簽章。在此繫結類型中,<message> 元素是定義文件格式,而非函數簽章。請參考下例中的 WSDL 片段:

<definitions
xmlns:stns="(SchemaTNS)"
xmlns:wtns="(WsdlTNS)"
targetNamespace="(WsdlTNS)">

<schema targetNamespace="(SchemaTNS)"
elementFormDefault="qualified">
<element name="SimpleElement" type="xsd:int"/>
<element name="CompositElement" type="stns:CompositeType"/>
<complexType name="CompositeType">
<all>
<element name='a' type="xsd:int"/>
<element name='b' type="xsd:string"/>
</all>
</complexType>
</schema>

<message...>
<part name='p1' type="stns:CompositeType"/>
<part name='p2' type="xsd:int"/>
<part name='p3' element="stns:SimpleElement"/>
<part name='p4' element="stns:CompositeElement"/>
</message>
?</definitions>

此結構描述有 SimpleElement 與 CompositeElement 兩個元素,以及一個宣告類型 (CompositeType)。所宣告的唯一 <message> 元素有四個部分 (part):p1 是 CompositeType 類型;p2 是 int 類型;p3 是 SimpleElement;而 p4 是 CompositeElement。下表是四種繫結的比較,依照「使用/類型」分別為:rpc/literal、document/literal、rpc/encoded、與 document/encoded。本表可說明,每種繫結方式反映在線上的實際情況。

rpc / literal
<operation name="method1" style="rpc" ...>
<input>
<soap:body parts="p1 p2 p3 p4"
use="literal"
namespace="(MessageNS)"/>
</input>
</operation>

線上情形:
<soapenv:body... xmlns:mns="(MessageNS)"
xmlns:stns="(SchemaTNS)">
<mns:method1>
<mns:p1>
<stns:a>123</stns:a>
<stns:b>hello</stns:b>
</mns:p1>
<mns:p2>123</mns:p2>
<mns:p3>

<stns:SimpleElement>

123

</stns:SimpleElement>
</mns:p3>
<mns:p4>
<stns:CompositeElement>
<stns:a>123</stns:a>
<stns:b>hello</stns:b>
</stns:CompositeElement>
</mns:p4>
</mns:method1>
</soapenv:body>
document / literal / type=
<operation name="method1"
style="document" ...>
<input>
<soap:body parts="p1" use="literal">
</input>
</operation>

線上情形:

<soapenv:body... xmlns:stns="(SchemaTNS)">
<stns:a>123</stns:a>
<stns:b>hello</stns:b>
</soapenv:body>

rpc / encoded
<operation name="method1" style="rpc" ...>
<input>
<soap:body parts="p1 p2" use="encoded"
encoding=
"http://schemas.xmlsoap.org/soap/encoding/"
namespace="(MessageNS)"/>
</input>
</operation>

線上情形:
<soapenv:body... xmlns:mns="(MessageNS)">
<mns:method1>
<p1 TARGET="_self" HREF="#1"/>
<p2>123</p2>
</mns:method1>
<mns:CompositeType id="#1">
<a>123</a>
<b>hello</b>
</mns:CompositeType>
</soapenv:body>
document / literal / element=
<operation name="method1"
style="document" ...>
<input>
<soap:body parts="p3 p4"

use="literal">
</input>
</operation>

線上情形:

<soapenv:body... xmlns:stns="(SchemaTNS)">
<stns:SimpleElement>

123

</stns:SimpleElement>
<stns:CompositeElement>
<stns:a>123</stns:a>
<stns:b>hello</stns:b>
</stns:CompositeElement>
</soapenv:body>

document / encoded
<operation name="method1"
style="document" ...>
<input>
<soap:body parts="p1 p2" use="encoded"
encoding=

"http://schemas.xmlsoap.org/soap/encoding/"
namespace="(MessageNS)"/>
</input>
</operation>

線上情形:
<soapenv:body... xmlns:mns="(MessageNS)">
<mns:CompositeType>
<a>123</a>
<b>hello</b>
</mns:CompositeType>
<soapenc:int>123</soapenc:int>
</soapenv:body>


<service> 與 <port> 元素
服務即是一組 <port> 元素。每個 <port> 元素,都以一對一的方式,關連一個位置與 <binding>。若有一個以上的 <port> 元素,與相同的 <binding> 關連,便可以使用其它 URL 作為替換。

在 WSDL 文件中,可以有一個以上的 <service> 元素。多個 <service> 元素,可以有許多用途。其中之一便是,根據 URL 的目的地,將傳輸埠群組化。因此,我只要使用另一個 <service>,就可以重新導向所有股票報價要求;而且我的客戶端程式依然可以正常運作,這是因為在這類的服務群組化中,通訊協定完全不會更動其它的服務。多個 <service> 服務的另一個用途是為了,根據基礎通訊協定,對傳輸埠進行分類。例如,可將所有的 HTTP 傳輸埠,置於一個 <service>;所有的 SMTP 傳輸埠置於另一個 <service>。客戶端便可根據它能處理的通訊協定,搜尋相對的 <service>。

<service name="FOOService">
<port name="fooSamplePort" binding="fooSampleBinding">
<soap:address
location="http://carlos:8080/fooService/foo.asp"/>
</port>
</service>

在一份 WSDL 文件中,<service> 的「name」屬性,可區分出個別服務的不同。因為一個服務可能會有幾個傳輸埠,所以傳輸埠也必須有「name」屬性。

總結
本文已經說明了 WSDL 文件最顯著的 SOAP 特色。但應該聲明的是,WSDL 絕不僅只能在 HTTP 上說明 SOAP 而已。在說明 SOAP 上,WSDL 的表達能力絕對足以使用 HTTP-POST、HTTP-GET、SMTP、與其它通訊協定。只要使用 WSDL,不論是程式開發人員或一般使用者,都可輕鬆處理 SOAP。個人深信,WSDL 與 SOAP 的結合,將可引進全新的應用程式類別,以更徹底地利用各種網路服務。

在 WSDL 的命名空間,有各種 XML 元素。下表整理了這些元素、屬性、與內容,以供參考:

元素 屬性 內容 (子項)
<definitions> name
targetNamespace
xmlns (其它命名空間) <types>
<message>
<portType>
<binding>
<service>
<types> (無) <xsd:schema>
<message> name <part>
<portType> name <operation>
<binding> name
type <operation>
<service> name <port>
<part> name
type (空值)
<operation> name
parameterOrder <input>
<output>
<fault>
<input> name
message (空值)
<output> name
message (空值)
<fault> name

message (空值)
<port> name
binding <soap:address>

資源:
WSDL 1.1
SOAP 1.1
XML 結構描述入門
MS SOAP Toolkit 下載網站
IDL 至 WSDL 的轉譯工具
含 WSDL 至 VB 之 Proxy 產生器的免費網路資源
PocketSOAP:SOAP 之相關元件、工具、與原始程式碼


原文地址:http://stu.timeover.net/article.php?articleid=842
posted on 2006-03-09 10:25 hopeshared 阅读(1208) 评论(0)  编辑  收藏 所属分类: JavaWeb Service

只有注册用户登录后才能发表评论。


网站导航: