使用Axis2搭建web Service

1、首先下载需要的一些相关axis2.zip及jar包
axis2:[url]http://axis.apache.org/axis2/java/core/download.cgi[/url]。
eclipse的官方提供的axis插件工具,可以打包aar文件和生成客户端调用代码:[url]http://axis.apache.org/axis2/java/core/tools/index.html[/url]。
下载其中的Service Archive Wizard - Eclipse Plug-in和Code Generator Wizard - Eclipse Plug-in([color=blue][size=x-small]本文也提供这两个jar下载[/size][/color])。
2、配置axis2及eclipse
2.1 eclipse中axis2环境配置:Window->Perferences->Web Services->Axis2 perferences->Axis2 Runtime->Axis2 Runtime location,Browse选择解压axis2-1.6.2-bin.zip得到的axis2-1.6.2文件目录。如图:
[img]http://dl2.iteye.com/upload/attachment/0097/8138/ec926f68-1a2b-3019-8f9b-794b563126cb.png[/img]
2.2 安装插件:解压axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip,把得到的两个jar包放入eclipse目录下的\plugins\中,重启eclipse。
3、用eclipse的axis2插件发布web服务
3.1 在eclipse中创建一个项目axis2,创建一个webservice服务器类,创建写完方法后编译一下

package axis.test;

public class Axis2Test {

public String axisTest(String name){
if(name==null){
name = "nobody";
}
return "hello,"+name;
}

}

3.2 在eclipse的空白workspace处,右键new->Other,在弹出的对话框中,找到Axis2 Service Archiver,双击->选择HelloWorldService所在项目的class路径,如图
[img]http://dl2.iteye.com/upload/attachment/0097/8140/52247ea0-b920-359f-95e3-ec28ca56b41c.jpg[/img]
然后next,选中skip WSDL,继续next,在next选中Generate the service xml automatically继续next,然后输入Service name、Class name(类全路径名)点击load,在下面的Method name中出现之前写的方法名就表示成功了,继续点击next,如图:
[img]http://dl2.iteye.com/upload/attachment/0097/8142/cbb53b42-1599-3d27-9368-225a233d3ba1.jpg[/img]
选择OutPut file location路径(项目的src根目录),点击Finish,如图:
[img]http://dl2.iteye.com/upload/attachment/0097/8144/b35c8efb-15f9-346c-85b1-ad7cd951035f.jpg[/img]
刷新项目后就能在src根目录下看见my_service.aar包了,在WEB-INF下新建services文件夹,将my_service.aar复制到该文件夹下。
将axis2项目下的WEB-INF下的conf文件夹复制到项目的WEB-INF下,修改里面的axis2.xml文件,将<module ref="addressing"/>这句注释掉。
将axis2项目中axis2-web文件夹复制到WebContent文件夹下,秩序保留其中的listServices.jsp([color=red][size=x-small]将里面include的页面除了httpbase.jsp外都删掉[/size][/color])及include文件夹下的httpbase.jsp。
在web.xml配置中添加如下配置:

<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-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>

发布项目,打开http://localhost:8080/axis2/services/listServices后可以看到之前写的webService类及方法了,点击服务名就可以看到wsdl内容了,如图:
[img]http://dl2.iteye.com/upload/attachment/0097/8392/24722c26-ea87-34c3-99b6-f1504bb160a0.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0097/8394/bb17bde0-e035-3213-86b9-3446105ad7d4.jpg[/img]

[size=large][b]调用webService[/b][/size]

package axis.test;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


public class Axis2Client {

/**
* @param args
*@data 2014-6-6 上午11:34:37
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/Axis2Test");
options.setTo(targetEPR);
//指定调用方法的参数值
Object[] opAddEntryArgs = new Object[] {"成功了"};
//指定调用方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
//指定要调用的方法名称及WSDL文件的命名空间
QName opAddEntry = new QName("http://test.axis", "axisTest");
//调用并输出返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
} catch (Exception e) {
e.printStackTrace();
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值