一直在找简单开发webservice的方法.幸亏找到了.其实只需要几步就能将你的应用中开放webservice接口.

axis2的热部署着实魅力很大,所以采用axis2.

我将几篇文章整合了下.

 官方网站:http://axis.apache.org/axis2/java/core/index.html

Apache Axis2 下载页面:http://axis.apache.org/axis2/java/core/download.cgi (当前最新版本1.6.2)

Apache Axis2 Binary Distribution(1.6.2):http://mirror.bjtu.edu.cn/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-bin.zip

 

WAR Distribution:http://mirror.bjtu.edu.cn/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-war.zip

Eclipse 插件:

Service Archive Wizard - Eclipse Plug-in(用来将服务代码打包成后缀名为.aar文件的插件):

http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.6.2/axis2-eclipse-service-plugin-1.6.2.zip

Code Generator Wizard - Eclipse Plug-in(用来将服务代码生成wsdl文件以及解析将wsdl文件生成客户端代码的插件):

http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.6.2/axis2-eclipse-codegen-plugin-1.6.2.zip

 

安装插件:

我用的是MyEclipse10,以我的环境为例:将两个插件解压后放到D:\sdk\MyEclipse\MyEclipse 10\dropins目录下面,启动MyEclipse

File - New - Other 可以找到:

Eclipse 开发  Apache axis2  WebService_java


 

三、部署axis2 war

解压下载的axis2-1.6.2-war.zip 得到一个axis2.war文件,将这个文件丢到tomcat\webapps目录,启动tomcat  访问http://localhost:8080/axis2

看到如下界面,部署成功。


Eclipse 开发  Apache axis2  WebService_Web_02


 

四、编写服务代码

1. package com.xcy;  
2.   
3. /** 
4.  * @author 肖纯勇(Siuon) 
5.  * @version 1.0 
6.  * @create 2012-7-19 下午8:23:49 
7.  */  
8. public class Axis2WB {  
9.     /** 
10.      * 提供了一个说Hello的服务 
11.      * @return 
12.      */  
13.     public String sayHello(String name){  
14.         return "Hello "+name;  
15.     }  
16.       
17.     /** 
18.      * 提供了一个做加法的服务 
19.      * @param a 
20.      * @param b 
21.      * @return 
22.      */  
23.     public int add(int a,int b){  
24.         return a + b;  
25.     }  
26.       
27. }


五、将服务代码打包成arr文件:

 

Eclipse菜单- New - File - Other -Axis2 Service Archiver

Eclipse 开发  Apache axis2  WebService_java_03


 

class file location:为刚刚写的Axis2WB类所在工程的bin目录

Eclipse 开发  Apache axis2  WebService_Web_04


 

选择skip wsdl

Eclipse 开发  Apache axis2  WebService_axis2_05


 

如果你的Axis2WB有引用jar包,则在这里选择。我写的没有,所以next

Eclipse 开发  Apache axis2  WebService_apache_06


 

由于我们没有编写service.xml,所以勾选让它自动生成,next

Eclipse 开发  Apache axis2  WebService_apache_07


 

输入服务名称(随意)、类全名、load、next

Eclipse 开发  Apache axis2  WebService_apache_08


 

设置aar文件名以及存放目录(我是放在桌面)--Finish:

Eclipse 开发  Apache axis2  WebService_apache_09


 

完成后,可以看到桌面上多了一个axis2wb.aar文件,我们用winrar打开:

Eclipse 开发  Apache axis2  WebService_java_10

Eclipse 开发  Apache axis2  WebService_java_11


 

有没有感觉很眼熟?很像一个jar包、我们点击META-INF目录进去,可以看到插件给我们生成的一个service.xml,打开看看(是不是明白插件做了些啥了):

  

Eclipse 开发  Apache axis2  WebService_Web_12


 

 

六、发布

将axis2wb.aar文件丢到之前部署的axis2应用的WEB-INF\services\目录下面,重启tomcat

再访问http://localhost:8080/axis2/   点击Service

Eclipse 开发  Apache axis2  WebService_apache_13


 

Eclipse 开发  Apache axis2  WebService_axis2_14


 

Eclipse 开发  Apache axis2  WebService_java_15


 

看到上图,说明发布成功

 

七、生成客户端代码

你可以用jdk6自带的wsimport工具生成客户端代码:Java 6 开发 WebService

也可以通过axis2 的Eclipse插件生成客户端代码:

Eclipse菜单-File-New-Other-Axis2 Code Generator

Eclipse 开发  Apache axis2  WebService_apache_16


 

Generate Java source code from a WSDL file:根据WSDL生成webservice客户端的java代码。(在这里,我们选择这个)
Generate a WSDL from a Java source file   :根据一个java源文件生成wsdl文件(这个源文件是打算发布成Web服务的java源文件,例如本demo中的Axis2WB.java)。

Eclipse 开发  Apache axis2  WebService_java_17


 

Eclipse 开发  Apache axis2  WebService_axis2_18


 

Eclipse 开发  Apache axis2  WebService_apache_19


 

Eclipse 开发  Apache axis2  WebService_axis2_20


 

生成完代码后,你会发现报错,原因是因为缺少相关的jar包。

解压在第一步中下载的axis2 binary.zip  将解压后的目录中的lib下面的所有jar包,拷进来,添加到class path中:

Eclipse 开发  Apache axis2  WebService_Web_21


 

 

 

8、调用Web服务

 

1. package test;  
2.   
3. import java.rmi.RemoteException;  
4.   
5. import com.xcy.Add;  
6. import com.xcy.AddResponse;  
7. import com.xcy.Axis2WB;  
8. import com.xcy.Axis2WBStub;  
9. import com.xcy.SayHello;  
10. import com.xcy.SayHelloResponse;  
11.   
12. /** 
13.  * @author 肖纯勇(Siuon) 
14.  * @version 1.0 
15.  * @create 2012-7-19 下午9:18:23 
16.  */  
17. public class Test {  
18.     public static void main(String[] args) throws RemoteException {  
19.         //创建客户端对象  
20.         Axis2WB axis2wb = new Axis2WBStub();  
21.           
22.         //new一个调用sayHello方法需要的参数SayHello,并且设置name  
23.         SayHello sayHello = new SayHello();  
24.         sayHello.setName("Siuon");  
25.         //调用web服务  
26.         SayHelloResponse sayHelloResponse = axis2wb.sayHello(sayHello);  
27.         //拿到返回结果  
28.         System.out.println(sayHelloResponse.get_return());  
29.           
30.           
31.         Add add = new Add();  
32.         add.setA(5);  
33.         add.setB(3);  
34.         AddResponse addResponse = axis2wb.add(add);  
35.         System.out.println(addResponse.get_return());  
36.     }  
37. }

结果:

 

Eclipse 开发  Apache axis2  WebService_java_22

 

 

整合Web应用与Axis2

整合Web应用与Axis2 
本文主要介绍了将Axis2整合到自己的Web应用程序中的过程。 

1.将axis2.war发布到%TOMCAT_HOME%\webapps中,发布后的目录为%TOMCAT_HOME%\webapps\axis2 
,该目录定义为%AXIS2_HOME%.
2.拷贝%AXIS2_HOME%\WEB-INF目录下所有内容到%MY_APPS%\WEB-INF目录下。
3.修改%MY_APPS%\WEB-INF目录下的web.xml文件,其中需要保留的axis2的配置内容如下:
<!-- Axis2 Service Servlet and servlet mappings -->
<!-- ==================================== -->
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- ==================================== -->


4.发布%MY_APPS%应用程序,发布Web Service到%MY_APPS%\WEB-INF\services\下,一般为.aar文件。
5.可以从客户端访问:http://localhost:8080/My_APPS/services/SomeService访问该Web Service.

 


Spring + axis2 开发 webservice

查看:http://yangzb.iteye.com/blog/663474