?
???轉載自? http://bldmickey.blog.sohu.com/59887565.html
二. 安裝Apache Axis2
詳細見《Apache Axis2安裝》 http://bldmickey.blog.sohu.com/56486087.html
?
三. 開發環境描述
軟件 |
版本 |
Eclipse SDK Version |
3.3.0 |
STP(SOA Tools Platform) |
0.6.0 |
WST(Web Standard Tools) |
2.0.0 |
DTP(Data Tools Platform) |
1.5.0 |
JST(J2EE Standard Tools) |
2.0.0 |
Apache Axis2 |
1.3 |
Apache Tomcat |
5.5.20 |
?
四. 下載Apache Axis2 Tools
- ???????? Code Generator Wizard - Eclipse Plug-in 版本1.3
包括Java2WSDL和WSDL2Java。其中Java2WSDL從java class開始創建WSDL文件;WSDL2Java從WSDL文件開始創建java class。下載地址:
http://apache.mirror.phpchina.com/ws/axis2/tools/1_3/axis2-eclipse-codegen-wizard.zip
- ??????? Service Archive Wizard - Eclipse Plug-in版本1.3
?????? 用來創建web服務的軟件包(aar文件或者jar文件)。下載地址:
http://apache.mirror.phpchina.com/ws/axis2/tools/1_3/axis2-eclipse-service-archiver-wizard.zip
?
五. Apache Axis2 Tools的安裝
- ???????? 解壓下載的文件
- ???????? 拷貝解壓的內容到eclipse安裝目錄下的plugins目錄下
?
六. Eclipse+Axis2集成例子
6.1 Eclipse Axis2參數設置
- ???????? 菜單選擇:Window->Preference…->Web Service->Axis2 Preferences
- ???????? 設置Axis Runtime Location,也就是Axis2 Runtime安裝的位置,例如:C:\Program Files\axis2
- ?????????? 設置Axis2 Preferences
?
6.2 創建Web Project
- ???????? 菜單選擇:File->New Project…->Web(Dynamic Web Project)選擇Next, 在項目名稱中輸入SayHi
- ???????? 選擇“Finish”完成
6.3 添加Axis2 Library
在項目屬性中->Java Build Path->Library中添加一個自定義的Axis2_Library的庫,將axis2 web應用WEB-INF/lib目錄中所有jar添加到新定義的庫中。添加后項目的情況:
?
6.4 創建WSDL文件
- ???????? 菜單選擇:File->New Other…->Web Service WSDL->next
- ???????? 選中指定路徑SayHi/src,指定WSDL的文件名:SayHi.wsdl,選擇Next
- ???????? Target namespace:修改成 http://www.wsexample.org/SayHi ,選擇Finish
- ?? 在WSDL design的tab中修改WSDL的設計。例如修改:Operation的名稱為:SayHiOper,具體有關WSDL的信息請查看《Web Service WSDL.doc》和《Web Service XML.doc》
?
- ??????? 例如:本例中采用的types定義如下:
<wsdl:types> ??? <xsd:schema targetNamespace="http://www.example.org/SayHi/"> ????? <xsd:element name="SayHiOper"> ??????? <xsd:complexType> ????????? <xsd:sequence> ??????????? <xsd:element name="in" type="xsd:string"/> ????????? </xsd:sequence> ??????? </xsd:complexType> ????? </xsd:element> ????? <xsd:element name="SayHiOperResponse"> ??????? <xsd:complexType> ????????? <xsd:sequence> ??????????? <xsd:element name="out" type="xsd:string"/> ????????? </xsd:sequence> ??????? </xsd:complexType> ????? </xsd:element> ??? </xsd:schema> ? </wsdl:types> |
?
6.5從WSDL文件生成Java Server代碼
- ? 選擇File->New->Other,從對話框中選取Axis2 Wizards->Axis2 Code Generator,選擇Next
- ?????? 選擇“Generate java source code from WSDL file”,點擊“Next”進入下一步
- ?????? 選擇WSDL文件所在的路徑:選中SayHi.wsdl文件
- ?????? Axis2 Codegen Wizard對話框中選擇:(完成后選擇Next繼續)
- ?????? Codegen Option為custom
- ???????Databinding Name為缺省的ADB(還有xmlbeans、jibx)
- ? ?????先創建服務器端代碼
- ????????確定java服務器端程序的輸出位置,例如:SayHi\(不包括src目錄)。生成的代碼輸出的位置。注意:在程序的目錄中,不需要指定到src目錄下。
- ??????? 項目程序結果:
?
6.6 修改Service代碼
在SayHiSkeleton.java程序中添加處理代碼(藍色部分),實現了SayHiOper函數接口,獲取web請求“sayHiOper.getIn() ”,然后添加相關問候信息“"Hi," request ".How are you? ”
package org.example.www.sayhi; public class SayHiSkeleton{ ?? public org.example.www.sayhi.SayHiOperResponse SayHiOper ??????????? ( org.example.www.sayhi.SayHiOper sayHiOper ) ?? ??{ ??????? ? try { ??????????????? SayHiOperResponse response = new SayHiOperResponse(); ??????????????? response.setOut("Hi,"+sayHiOper.getIn()+". How are you?"); ??????????????? return response; ?????????????? } catch (UnsupportedOperationException e) ?????????? ????{ throw e;} ??????? //throw new? java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#SayHiOper"); ??????? } ??? } |
?
6.7 創建發布Package
- ??? 菜單選擇:File->New->Other…->Axis2 Wizards中選擇Axis2 Service Archive,選擇Next
- ??? 選擇classes的路徑,例如:SayHi\build\classes,選擇Next
- ??? 選擇WSDL文件,選擇Next,在選擇Next
- ??? 設置Service XML文件,例如:SayHi\resources\services.xml
- ??? 設置輸出的文件和目錄,例如tomcat下的部署目錄:webapps\axis2\WEB-INF\services,輸出文件名:SayHi.aar,直接可以輸出到tomcat對應的axis2 services的目錄中。
6.8 瀏覽器測試
- ??? 啟動tomcat
- ??? 瀏覽器器中輸入service的WSDL,鏈接地址: http://localhost:8081/axis2/services/SayHi?wsdl ,輸出結果如下:
- ??測試Web Service,在瀏覽器中輸入: http://localhost:8081/axis2/services/SayHi/SayHiOper?in=bldmickey ,輸出結果如下:
<ns1:SayHiOperResponse xmlns:ns1=” http://www.example.org/SayHi/ "> ? <out> Hi,bldmickey. How are you? </out> </ns1:SayHiOperResponse> |
?????? 其中SayHiOper是對應的Web Service的Operation,in對應的是函數對應的輸入值。詳細對應在wsdl文件中。輸入參數的值是:bldmickey,輸出的結果是: Hi,bldmickey. How are you?
?
6.9 從WSDL生成Java Client代碼
- ?? 選擇File->New->Other,從對話框中選取Axis2 Wizards->Axis2 Code Generator,選擇Next
- ?? 具體的操作和6.5中創建Server端代碼類似,只是選擇輸出客戶端代碼,具體見下圖:
?
- ? 確定java客戶端程序的輸出位置,例如:SayHi
- ? 增加JUNIT庫(測試代碼需要JUNIT庫)
- ? 項目屬性->Java Build Path->Libraries中Add Library…->選擇JUnit, Next繼續,缺省選擇JUnit3完成
- ? 創建的文件包括:SayHi\src\org\example\www\sayhi\SayHiStub.java(Stub程序)和SayHi\test\org\example\www\sayhi\SayHiTest.java(測試程序)?
6.10 修改客戶端代碼和測試
- ?? 修改test代碼的路徑問題,將SayHiTest.java從test\org\example\www\sayhi目錄移動到src\org\example\www\sayhi\目錄
- ?? 修改代碼
package org.example.www.sayhi; public class SayHiTest extends junit.framework.TestCase{ ??????? public? void testSayHiOper() throws java.lang.Exception{ ??????? String url="http://localhost:8081/axis2/services/SayHi"; ??????? org.example.www.sayhi.SayHiStub stub = ??????? ?????? ?????????? new org.example.www.sayhi.SayHiStub(url); ??????? //org.example.www.sayhi.SayHiStub stub = ??????? //????????? ???new org.example.www.sayhi.SayHiStub(); ??????? org.example.www.sayhi.SayHiStub.SayHiOper sayHiOper2= ?????????????????? (org.example.www.sayhi.SayHiStub.SayHiOper)getTestObject(org.example.www.sayhi.SayHiStub.SayHiOper.class); ??????? sayHiOper2.setIn("Client bldmickey"); ??????? System.out.println(stub.SayHiOper(sayHiOper2).getOut()); ??????? assertNotNull(stub.SayHiOper(sayHiOper2)); ??????? //? assertNotNull(stub.SayHiOper(sayHiOper2)); ? ?????} ??????? //Create an ADBBean and provide it as the test object ??????? public org.apache.axis2.databinding.ADBBean getTestObject(java.lang.Class type) throws Exception{ ?????????? return (org.apache.axis2.databinding.ADBBean) type.newInstance(); ??????? } ??? } ?} ??? |
- ???????? 執行Run As->JUnit Test
- Console 中輸出: Hi,Client bldmickey. How are you?
?
6.11 TCP Monitor監控Web Service
運行tcpmon.bat
添加監聽8888端口,定向到127.0.0.1端口8081
瀏覽器中訪問 http://localhost:8888/axis2/services/SayHi/SayHiOper?in=bldmickey
輸入: GET /axis2/services/SayHi/SayHiOper?in=bldmickey HTTP/1.1 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* Accept-Language: en-us UA-CPU: x86 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727) Host: 127.0.0.1:8888 Connection: Keep-Alive |
輸出 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/xml;charset=UTF-8 Transfer-Encoding: chunked Date: Wed, 15 Aug 2007 06:03:12 GMT 7e <ns1:SayHiOperResponse xmlns:ns1="http://www.example.org/SayHi/"><out>Hi,bldmickey. How are you?</out></ns1:SayHiOperResponse> 0 ? |
?
6.12 代碼分析
Axis2的新架構,寫一個服務,會有很多種不同的方法,可以用pojo結合rpc模式來寫,也可以用Axis2自己的Axiom API從底層寫,也可以從WSDL生成相應框架,然后填寫相應邏輯。
對于WSDL生成框架代碼這種形式,又有不同的數據綁定可以使用。Axis2.1.1 可以使用adb(axis2自己的data binding)、xmlbeans、jibx、jaxme、jaxbri。其中常用的還是前兩種,axis2官方是極力推薦自己的data binding的,因為速度快,架構相對簡單,而且有一些獨有的特性。xmlbeans,是對模式支持最好的data binding,一些特別復雜的模式只能在xmlbeans下才能正確生成框架代碼。其他幾種代碼wsdl2java目前都不是支持得很好,都還在實驗改進階段。
面對這么多數據綁定模式,很多人都無從選擇。官方maillist上也有好多人問該用哪個。查了一些資料,據說,adb很好用,而且速度快,如果只是簡單的模式,那么使用這種數據綁定就可以了。xmlbeans的強力模式支持,可以支持足夠負責的模式,速度上也只是比adb低5%左右。而jibx很多人沒用過,因為使用沒有前兩種方便,不過jibx支持unwrapping代碼生成(普通代碼生成的服務方法,一般只有一個參數,而unwrapping可以生成多個參數的方法,相當直觀。)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
