Myeclipse+Axis2+Tomcat開發webService

一、  下載文件:java


須要在axis2官網下載兩種類型的axis2文件,bin版和war版(下載地址:http://axis.apache.org/axis2/java/core/download.cgi),bin版中包含了開發所需的jar文件,而war則用於部署在%TOMCAT_HOME%\webapps\目錄下。
eclipse-codegen和eclipse-service爲安裝myeclipse開發axis2所須要的插件文件(下載地址:http://archive.apache.org/dist/ws/axis2/tools/1_4_1/ )。web

二、  把axis2-1.4.1-war.zip中的war文件複製到%TOMCAT_HOME%\webapps\目錄下,啓動tomcat,在地址欄訪問:http://127.0.0.1:8080/axis2/,出現以下界面:apache


axis2安裝成功。api

三、  Myeclipse Axis2插件:分別解壓
axis2-eclipse-codegen-wizard.zip 和 axis2-eclipse-service-archiver-wizard.zip兩個文件到%ECLIPSE_HOME%\eclipse\plugins目錄中。在%ECLIPSE_HOME\eclipse\links%目錄下增長文件axis-eclipse-plugin.link並寫入path=%ECLIPSE_HOME%\eclipse\plugins(不可直接複製,須要將對應的ECLIPSE_HOME更換爲實際值)。從新啓動myeclipse,在file->new->other中便可看到Axis2 Wizards,至此,axis2插件安裝成功。tomcat

四、  插件存在bug,在利用codegen插件根據WSDL文件生成stub類時,會報An error occurred while completing process -java.lang.reflect.InvocationTargetException異常,爲了解決此問題:從AXIS2的LIB庫中複製
"geronimo-stax-api_1.0_spec-1.0.1.jar"和"backport-util-concurrent-3.1.jar"文件到Codegen的lib目錄中,同時修改plugin.xml文件,添加
<library name="lib/geronimo-stax-api_1.0_spec-1.0.1.jar">
         <export name="*"/>
</library>
<library name="lib/backport-util-concurrent-3.1.jar">
        <export name="*"/>
</library>
注意:因爲已經將插件解壓到了myeclipse的plugins目錄,故須要修改plugins目錄下對應文件中的插件數據。app

五、  Demo:
 a. 編寫服務端用於做爲webservice的類HelloDemo.java:eclipse

 1 package com.hxl.webservice.service;  2 
 3 public class HelloDemo {  4 
 5     public String sayHello(String name) {  6         return "Hello"+name;  7  }  8     
 9     public String getResp() { 10         return "請求被響應"; 11  } 12 }

 b. 發佈服務:右擊src目錄,選擇New-other-Axis2 Wizards-Axis2 Service Archiver,點擊next,選擇class文件所在目錄,例如:webapp


    點擊next,選擇Skip WSDL,點擊next,此處爲選擇服務所須要的依賴包,此demo不須要,點擊next,選中Generate the service xml automatically,讓myeclipse自動生成services.xml,點擊next,此處經過設定Service Name文本框設定服務名,Class Name選擇須要發佈爲服務的類,點擊next,Output file location用於設定配置服務的配置文件生成的路徑,應爲%TOMCAT_HOME%\webapps\axis2\WEB-INF\services目錄,output File Name用於設定輸出的配置文件的名稱,點擊Finish,至此service服務發佈工做所有完成,訪問:http://127.0.0.1:8080/axis2/services/listServices,便可查看剛發佈的service。
 c. 生成stub類:右擊src目錄,選擇New-other-Axis2 Wizards-Axis2 Code Generator,點擊next,選中Generate Java source code from a WSDLfile,點擊next,WSDL file location輸入框中輸入剛剛發佈的服務的wsdl地址:http://127.0.0.1:8080/axis2/services/MyService?wsdl,點擊next,此處默認便可,默認會生成同步和異步調用的stub類,點擊next,選中Browse and select a project on current eclipse workspace,在output path選擇框中選擇當前的項目,點擊Finish,點擊OK,刷新項目,會看到生成了java類,自此,全部stub生成工做已經完成。
 d. 編寫測試類:異步

 1 package com.hxl.webservice.stub;  2 
 3 import com.hxl.webservice.stub.MyServiceStub.GetRespResponse;  4 import com.hxl.webservice.stub.MyServiceStub.SayHelloResponse;  5 
 6 public class MyCallBack extends MyServiceCallbackHandler {  7 
 8  @Override  9     public void receiveResultgetResp(GetRespResponse result) { 10  System.out.println(result.local_return); 11  } 12 
13  @Override 14     public void receiveResultsayHello(SayHelloResponse result) { 15  System.out.println(result.local_return); 16  } 17 
18     
19 }
 1 package com.hxl.webservice.test;  2 import org.junit.Test;  3 
 4 import com.hxl.webservice.stub.MyCallBack;  5 import com.hxl.webservice.stub.MyServiceStub;  6 
 7 public class TestHelloDemo {  8  @Test  9     /**
10  * 測試同步getResp()方法 11      */
12     public void testGetResp() throws Exception { 13         
14         MyServiceStub stub = new MyServiceStub(); 15         MyServiceStub.GetResp gr = new MyServiceStub.GetResp(); 16  System.out.println(stub.getResp(gr).get_return()); 17  } 18  @Test 19     /**
20  * 測試同步sayHello()方法 21      */
22     public void testSayHello() throws Exception { 23         
24         MyServiceStub stub  = new MyServiceStub(); 25         MyServiceStub.SayHello sh = new MyServiceStub.SayHello(); 26         sh.setName(" hxl"); 27  System.out.println(stub.sayHello(sh).get_return()); 28  } 29  @Test 30     /**
31  * 異步測試兩個方法 32      */
33     public void testGetAyn() throws Exception { 34         MyServiceStub stub = new MyServiceStub(); 35         MyServiceStub.GetResp gr = new MyServiceStub.GetResp(); 36         stub.startgetResp(gr ,new MyCallBack()); 37         MyServiceStub.SayHello sh = new MyServiceStub.SayHello(); 38         sh.setName(" Darren!"); 39         stub.startsayHello(sh, new MyCallBack()); 40         System.out.println("異步調用"); 41  System.in.read(); 42  } 43     
44 }
相關文章
相關標籤/搜索