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