級(jí)別: 中級(jí)
Nicholas Chase
(ibmquestions@nicholaschase.com), 自由作者, Backstop Media, LLC
2006 年 5 月 25 日
使用 Apache Geronimo 和 Axis2 構(gòu)建復(fù)雜 Web 服務(wù)應(yīng)用程序是一項(xiàng)錯(cuò)綜復(fù)雜的任務(wù),本系列將深入研究相關(guān)內(nèi)容。本系列教程由 3 個(gè)部分構(gòu)成,引領(lǐng)您構(gòu)建一個(gè)示例在線銀行 Web 服務(wù),文中介紹了整個(gè)流程的每個(gè)步驟,因此新用戶(hù)可快速掌握概念并構(gòu)建一個(gè)完整的 Web 服務(wù)和可與 Web 服務(wù)連接并通信的 Web 界面客戶(hù)機(jī)。在第一期中,將為您介紹示例 Web 服務(wù)和使用 WSDL 的 Web 服務(wù),構(gòu)建并編譯 WSDL 文件,然后在 Geronimo 上測(cè)試及部署它。
開(kāi)始之前
本系列教程分 3 部分,面向?qū)κ褂?Apache Geronimo 和 Apache Axis2 開(kāi)發(fā)大型 Web 服務(wù)有興趣的讀者。了解如何構(gòu)建 WSDL 文件及與在線銀行系統(tǒng)相關(guān)的一些操作。然后使用 Axis2 將 WSDL 文件編譯為 Web 服務(wù),并在 Geronimo 上進(jìn)行部署和測(cè)試。
關(guān)于本系列
本系列中將創(chuàng)建一個(gè)在線銀行 Web 服務(wù)示例。使用 WSDL 和 Axis2 創(chuàng)建的 Web 服務(wù)將包含一個(gè) Apache Derby 數(shù)據(jù)庫(kù),用于保存交易數(shù)據(jù)、賬戶(hù)數(shù)據(jù)和賬單支付數(shù)據(jù)。您會(huì)將 Axis2 作為 Apache Geronimo 上的 Web 應(yīng)用程序部署,新 Web 服務(wù)將是在 Axis2 中部署的 Axis 歸檔。您在示例中可能會(huì)用到在線銀行中的多個(gè)命令,包括查看賬戶(hù)、搜索交易、劃撥資金和支付賬單。您還會(huì)創(chuàng)建一個(gè)客戶(hù)機(jī) Web 應(yīng)用程序來(lái)連接到 Web 服務(wù),以查詢(xún)信息及執(zhí)行交易 —— 主要是為了調(diào)用在 Web 服務(wù)中實(shí)現(xiàn)的操作。
在本系列的第 1 部分中,將在 WSDL 中構(gòu)建在線銀行 Web 服務(wù)的基礎(chǔ)設(shè)施。在第 2 部分中,將通過(guò)在 Apache Derby 數(shù)據(jù)庫(kù)中存儲(chǔ)賬戶(hù)和交易信息來(lái)構(gòu)建 Web 服務(wù)的功能。在最后的第 3 部分中,將為 Web 服務(wù)創(chuàng)建功能完整、基于 Web 的客戶(hù)機(jī)用戶(hù)界面(UI),用戶(hù)可在其中登錄、進(jìn)行在線銀行活動(dòng)及注銷(xiāo)。
關(guān)于本教程
本教程首先介紹示例 Web 服務(wù),并展示使用 WSDL 的多種 Web 服務(wù)。您將使用 Axis2 將 WSDL 文件轉(zhuǎn)換為 Java? 代碼,其中將實(shí)現(xiàn)一些簡(jiǎn)單的功能,以測(cè)試客戶(hù)機(jī)與 Web 服務(wù)之間的通信。測(cè)試環(huán)境包括作為 Geronimo 上的模塊部署的 Axis2、作為 Axis2 內(nèi)的歸檔部署的 Web 服務(wù),以及與 Web 服務(wù)通信的一個(gè)簡(jiǎn)單 Java 客戶(hù)機(jī)。
前提條件
要學(xué)習(xí)本教程,需要具備以下工具:
- Apache Geronimo —— 下載 Apache Geronimo V1.0,您將在 Geronimo 上部署 Axis2 Web 服務(wù)。
- Apache Axis2 —— 下載 V0.94 Axis2 WAR 及二進(jìn)制發(fā)行版,用于從 WSDL 構(gòu)建 Web 服務(wù)。
- Apache Ant —— 下載 Apache Ant,由于 Axis2 的 WSDL2Java 工具為構(gòu)建 Web 服務(wù)創(chuàng)建了一個(gè)便捷的 Ant build.xml 文件,因此您需要用到此工具,這將使 Web 服務(wù)源代碼的構(gòu)建更容易。
- Java 代碼 —— 下載最新版的 Java 1.4,Java 1.4.10,Geronimo、Axis2 和 Ant 都需要用到它。
本教程假設(shè)您具備 Java 語(yǔ)法和編寫(xiě)代碼的基本知識(shí),但不必了解 Axis2 或特定 Geronimo 的相關(guān)知識(shí)。
在線銀行 Web 服務(wù)概述
下面簡(jiǎn)要介紹本系列教程的最終成果 —— 在線銀行 Web 服務(wù)示例。該 Web 服務(wù)涵蓋了在線銀行體驗(yàn)的幾種特性,包括查看賬戶(hù)和交易、劃撥資金、申請(qǐng)貸款和在線支付賬單。
賬戶(hù)
登錄到 Web 服務(wù)之后,您會(huì)看到您所擁有的賬戶(hù)及其余額。出于安全性方面的考慮,登錄過(guò)程包括發(fā)送用戶(hù)名和密碼,以及接收保證安全性的二進(jìn)制令牌。因此,在您登錄之后,僅需發(fā)送該令牌即可使 Web 服務(wù)識(shí)別您的身份。之后您就可以單擊查看賬戶(hù)中的交易,見(jiàn)下節(jié)。
交易
您在銀行中做的每一件事都涉及交易,各交易的歷史 —— 可在各賬戶(hù)中查看其歷史 —— 記錄在您的賬戶(hù)中。Web 服務(wù)還使您根據(jù)支票編號(hào)、金額或日期搜索交易。這使您可在納稅時(shí)更輕松地回溯和查找賬戶(hù)中的交易。您可將資金從一個(gè)賬戶(hù)劃撥到另外一個(gè)賬戶(hù)中,這也作為賬戶(hù)的一項(xiàng)交易記入日志。
申請(qǐng)貸款
本特性允許您通過(guò)在線銀行 Web 服務(wù)申請(qǐng)貸款。您可輸入貸款類(lèi)型、欲貸金額、償還時(shí)間。成功提交貸款申請(qǐng)表后,您可查看每筆貸款的狀態(tài)。
支付賬單
在線賬單支付服務(wù)現(xiàn)已非常普及。這種服務(wù)使您能夠通過(guò)銀行賬戶(hù)支付賬單。但在支付賬單之前,必須首先添加作為支付對(duì)象的人或公司,我們稱(chēng)之為收款方(payee)。Web 服務(wù)允許您查看、添加和編輯賬戶(hù)中的收款方。在賬戶(hù)中設(shè)置好收款方后,您就可以向其償還債務(wù)或劃撥資金了。所支持的支付類(lèi)型有兩種:一次性支付 和 定期支付。您可通過(guò) Web 服務(wù)執(zhí)行各類(lèi)支付。每次支付都有一個(gè)與其相關(guān)的日期,在支付發(fā)生之前(待定支付),允許您編輯其內(nèi)容。
在下一節(jié)中,我們將開(kāi)始構(gòu)建在線銀行 Web 服務(wù)的 WSDL。
數(shù)據(jù)結(jié)構(gòu)
您在 Web 服務(wù)中支持的消息及操作需要一些數(shù)據(jù)結(jié)構(gòu)用于來(lái)回傳遞數(shù)據(jù),在 WSDL 中也稱(chēng)為復(fù)型。這些數(shù)據(jù)結(jié)構(gòu)是通過(guò)簡(jiǎn)單對(duì)象訪問(wèn)協(xié)議(SOAP)傳送的,并且是使用 WSDL 定義的。Web 服務(wù)的操作需要 6 個(gè)數(shù)據(jù)結(jié)構(gòu),我們將從最常用的一種開(kāi)始,即交易。
交易
在您的在線銀行 Web 服務(wù)上下文中,兩個(gè)賬戶(hù)(或一個(gè)賬戶(hù)和銀行)是所涉兩方。就我們的在線銀行應(yīng)用程序而言,一個(gè)交易可能有 5 項(xiàng)屬性。下面看看這些屬性,參見(jiàn) 清單 1。現(xiàn)在創(chuàng)建一個(gè) WSDL 文件 —— OnlineBanking.wsdl,將 Transaction complexType
放置在其中。
Transaction complexType
中的 5 個(gè)元素在 清單 1 中顯示為黑體。所有必需元素均以 minOccurs="1" maxOccurs="1"
表示。這表示該元素號(hào)應(yīng)至少出現(xiàn)一次,且最多出現(xiàn)一次,也就是恰好一次。注意 checknumber
非必需,因?yàn)?minOccurs="0"
,這表示在一個(gè) Transaction
中,該元素可被忽略,也可以恰好出現(xiàn)一次。另外,查看數(shù)據(jù)類(lèi)型。注意,號(hào)碼定義為 int
、checknumber
和 int
、date
和 date
、header
和 string
,以及 amount
和 float
。
清單 1. 交易
<xsd:complexType name="Transaction">
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1"
name="number" type="xsd: int" />
<xsd:element minOccurs="0" maxOccurs="1"
name="checknumber" type="xsd: int" />
<xsd:element minOccurs="1" maxOccurs="1"
name="date" type="xsd: date" />
<xsd:element minOccurs="1" maxOccurs="1"
name="header" type="xsd: string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="amount" type="xsd: float" />
</xsd:sequence>
</xsd:complexType>
|
接下來(lái)將介紹收款方復(fù)型。
收款方
收款方接受您通過(guò)在線銀行支付的賬單。因此,需要幾個(gè)字段來(lái)獲得地址和賬單金額信息。為您的 WSDL 文件添加收款方復(fù)型,參見(jiàn) 清單 2。
清單 2. 收款方
<xsd:complexType name="Payee">
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1"
name="name" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="address1" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1"
name="address2" type="xsd:string" />
<xsd:element minOccurs="0" maxOccurs="1"
name="address3" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="city" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="state" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="zip" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="phoneAreaCode" type="xsd:int" />
<xsd:element minOccurs="1" maxOccurs="1"
name="phonePrefix" type="xsd:int" />
<xsd:element minOccurs="1" maxOccurs="1"
name="phoneSuffix" type="xsd:int" />
<xsd:element minOccurs="1" maxOccurs="1"
name="accountToPay" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="nameOnBill" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="alias" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
|
對(duì)于郵匯,您可以想到,需要的所有必需數(shù)據(jù),在 清單 2 中以黑體顯示。注意 address2
和 address3
,補(bǔ)充的額外地址行非必需。接下來(lái)定義貸款申請(qǐng)表復(fù)型。
貸款申請(qǐng)表
如果銀行不發(fā)放貸款,我們要如何生存?這里定義的數(shù)據(jù)結(jié)構(gòu)包括貸款申請(qǐng)表信息和貸款狀態(tài)信息。我們首先定義貸款申請(qǐng)表復(fù)型,參見(jiàn) 清單 3。
清單 3. 貸款申請(qǐng)表
<xsd:complexType name="LoanApplication">
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1"
name="loanType" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="amount" type="xsd:float" />
<xsd:element minOccurs="1" maxOccurs="1"
name="termMonths" type="xsd:int" />
<xsd:element minOccurs="1" maxOccurs="1"
name="joint" type="xsd:boolean" />
<xsd:element minOccurs="1" maxOccurs="1"
name="percentFinanced" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
|
各字段都包括貸款申請(qǐng)表中的典型數(shù)據(jù)。接下來(lái)將貸款狀態(tài)添加到您的 WSDL 中,如 清單 4 所示。
清單 4. 貸款申請(qǐng)表狀態(tài)
<xsd:complexType name="LoanApplicationStatus">
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1"
name="loanNumber" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="approved" type="xsd:boolean" />
<xsd:element minOccurs="1" maxOccurs="1"
name="denied" type="xsd:boolean" />
</xsd:sequence>
</xsd:complexType>
|
每筆貸款都獲得一個(gè)號(hào)碼以及一個(gè)與其相關(guān)的批準(zhǔn)或拒絕狀態(tài)。如果它既未得到批準(zhǔn),也未被拒絕,則該貸款尚未處理。接下來(lái)介紹與賬單支付相關(guān)的結(jié)構(gòu)。
賬單支付
支付賬單要求您輸入信息,如支付金額、支付對(duì)象,以及是否為定期支付等。接下來(lái)的兩個(gè)清單展示了兩種結(jié)構(gòu),供您參考。向您的 WSDL 文件中添加賬單支付復(fù)型,參見(jiàn) 清單 5。
清單 5. 賬單支付
<xsd:complexType name="BillPayment">
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1"
name="payeeName" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="account" type="xsd:int" />
<xsd:element minOccurs="1" maxOccurs="1"
name="amount" type="xsd:float" />
<xsd:element minOccurs="1" maxOccurs="1"
name="date" type="xsd:date" />
<xsd:element minOccurs="1" maxOccurs="1"
name="notes" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="recurring" type="xsd:boolean" />
<xsd:element minOccurs="0" maxOccurs="1"
name="recurringData"
type="xsd1:RecurringBillPayment" />
</xsd:sequence>
</xsd:complexType>
|
這里顯示的金額即為您所支付的金額。如果是定期支付,則此金額為您每次支付的金額,所以日期將為起始日期。另外,注意這里 recurring
指定支付是否為定期支付,如果是,則定義 recurringData
(注意其 minOccurs
定義為 0
)。recurringData
元素引用 RecurringBillPayment
復(fù)型,如 清單 6 所示。將其添加到您的 WSDL 中。
清單 6. 追加定期賬單支付數(shù)據(jù)
<xsd:complexType name="RecurringBillPayment">
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1"
name="finalPayment" type="xsd:float" />
<xsd:element minOccurs="1" maxOccurs="1"
name="frequency" type="xsd:string" />
<xsd:element minOccurs="1" maxOccurs="1"
name="totalNumberOfPayments" type="xsd:int" />
<xsd:element minOccurs="1" maxOccurs="1"
name="recurringType" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
|
清單 6
中的數(shù)據(jù)直接填充 清單 5 所示復(fù)型。在這里,您的最后支付為 finalPayment
字段的值,frequency
則為支付賬單的周期。recurringType
字段表示賬單金額是否為固定數(shù)量,也就是說(shuō)表示您是否可在下一次提交之前更改金額。接下來(lái)為您介紹需要通過(guò) WSDL 公開(kāi)的幾種不同操作。
posted on 2006-12-29 19:38
SIMONE 閱讀(1824)
評(píng)論(1) 編輯 收藏 所屬分類(lèi):
AXIS 、
JAVA