Myeclipse+Axis2+Tomcat开发webService

1、  下载文件:


需要在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/ )。

2、  把axis2-1.4.1-war.zip中的war文件复制到%TOMCAT_HOME%\webapps\目录下,启动tomcat,在地址栏访问:http://127.0.0.1:8080/axis2/,出现如下界面:


axis2安装成功。

3、  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插件安装成功。

4、  插件存在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目录下对应文件中的插件数据。

5、  Demo:
 a. 编写服务端用于作为webservice的类HelloDemo.java:

 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文件所在目录,例如:


    点击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 }

 

posted @ 2013-10-09 09:53  绝尘的神马  阅读(1740)  评论(0编辑  收藏  举报