zz基于Tomcat5.0和Axis2开发Web Service应用实例

[size=large][b]版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。[url]http://zhangjunhd.51cto.com/113473/23690[/url][/b][/size]

本文将介绍如何使用Tomcat5.0和Apache Axis2开发、部署及测试一个简单的Web Service应用。

1.工作环境
Eclipse Java EE for Web Developers
JDK1.6
apache-tomcat-6.0.20
axis2:1.5(war版本和bin版本)

下载AXIS2的Binary Distribution和war Distribution,把这两个文件解压,
比如解压缩的后得目录为C:\axis2-bin和C:\axis2.war。

在Eclipse下通过菜单window—preferences…--Java—Build Path—User Libraries 新建一个user library,比如名字就叫axis2把C:\axis2-std-1.0-bin\lib下的所有jar文件包含进来。把axis2.war拷贝到%TOMCAT-HOME%/webapps下面。

2.检验安装
在Eclipse下启动Tomcat,在地址栏内输入http://localhost:8080/axis2/。
[img]http://dl.iteye.com/upload/attachment/273035/ad72569f-86f9-3cb6-9983-1e5b93339428.jpg[/img]
点击Validate,将到达 Axis2 Happiness Page。
[img]http://dl.iteye.com/upload/attachment/273037/e9322777-ac96-325e-85ae-df96d2fd2991.jpg[/img]
3.WebService中的HelloWorld
1)新建一个动态web工程,取名ZZaxis,右键点击项目名,选择Properties-Java Build Path-Add Library-User Library-axis2。

2)新建package sample,建立HelloWorld.java,代码如下。
HelloWorld.java

package sample;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;<o:p></o:p>

public class HelloWorld {
public OMElement sayHello(OMElement in){
String name=in.getText();
String info=name+"HelloWorld!";
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
resp.setText(info);
return resp;
}
}


3)在WebContent\META-INF\建立services.xml,代码如下。
services.xml

<service name="HelloWorld">
<description>
This is a sample Web Service.
</description>
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
<operation name="sayHello">
<messagereceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver">
</messagereceiver>
</operation>
</service>


4)将目录sample和目录META-INF组织如下(新建目录example)。
+-example
|-------- +-sample
|------- HelloWorld.class
|---------+-META-INF
|------- services.xml

5)打包生成aar文件。
在命令符环境下,将当前目录转到example。
jar cvf HelloWorld.aar . //注意最后一个点,在当前目录下生成HelloWorld.aar。
[img]http://dl.iteye.com/upload/attachment/273039/1ed951a8-cfec-3b12-b14c-509abf1ca09b.jpg[/img]

6)在Eclipse中启动Tomcat,在地址栏下键入http://localhost:8080/axis2/。选择Administration,输入用户名admin,密码axis2。选择左侧工具栏Tools- Upload Service,上传之前打包的HelloWorld.aar。该文件将在<catalina_home></catalina_home>/webapps/axis2\WEB-INF\services目录下。

7)编写客户端检验代码。新建Java Project,取名为ZZaxisClient。右键点击项目名,选择Properties-Java Build Path-Add Library-User Library-axis2。

8)新建package example.client。建立TestClient.java,代码如下。
TestClient.java

package example.client;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;<o:p></o:p>
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;<o:p></o:p>
import org.apache.axis2.client.ServiceClient;<o:p></o:p>

public class TestClient {
private static EndpointReference targetEPR=new EndpointReference
("http://localhost:8080/axis2/services/HelloWorld");
public static OMElement getSayHelloOMElement(){
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");<o:p></o:p>
OMElement method=fac.createOMElement("sayHello",omNs);
method.setText("ZJ");
return method;
}
public static void main(String[] args){
try{
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
OMElement sayHello=TestClient.getSayHelloOMElement();
OMElement result=sender.sendReceive(sayHello);
System.out.println(result);
}
catch(Exception axisFault){
axisFault.printStackTrace();
}
}
}


9)测试,run TestClient.java as Java Application。结果:
<hw:sayHelloResponse xmlns:hw="http://helloworld.com/">ZJHelloWorld!</hw:sayHelloResponse>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值