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

本文将介绍如何使用Tomcat5.0 Apache Axis2 开发、部署及测试一个简单的Web Service 应用。
author: ZJ 07-3-12
Blog: [url]http://zhangjunhd.blog.51cto.com/[/url]
 
1 工作环境
Eclipse 3.1.2 +Lomboz+jdk1.5+ apache-tomcat-5.0.18+AXIS2:1.0(war 版本和 bin 版本 )
[url]http://ws.apache.org/axis2/download/1_0/download.cgi[/url] 页面 下载 AXIS2 Binary Distribution url: [url]http://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip[/url] war Distribution url: [url]http://apache.justdn.org/ws/axis2/1_0/axis2-1.0-docs.zip[/url] 。把这两个文件解压,比如解压缩的后得目录为 C:\axis2-std-1.0-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 ,在地址栏内输入 [url]http://localhost:8080/axis2/[/url]
点击 Validate ,将到达 Axis2 Happiness Page
3 WebService 中的HelloWorld
1 )新建一个动态 web 工程,取名 ZZaxis ,右键点击项目名,选择 Properties-Java Build Path-Add Library-User Library-axis2
 
2 )新建 package sample ,建立 HelloWorld.java ,代码如下。
HelloWorld.java
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
 
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
<?xml version="1.0" encoding="UTF-8"?>
<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"/>
</operation>
</service>
 
4 )将目录 sample 和目录 META-INF 组织如下(新建目录 example )。
+-example
|-------- +-sample
    |------- HelloWorld.class
|---------+-META-INF
       |------- services.xml
 
5 )打包生成 aar 文件。
在命令符环境下,将当前目录转到 example
jar cvf HelloWorld.aar . // 注意最后一个点,在当前目录下生成 HelloWorld.aar
 
6 )在 Eclipse 中启动 Tomcat ,在地址栏下键入 [url]http://localhost:8080/axis2/[/url] 。选择 Administration ,输入用户名 admin ,密码 axis2 。选择左侧工具栏 Tools- Upload Service ,上传之前打包的 HelloWorld.aar 。该文件将在 <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;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
 
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");
              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/"
xmlns:tns="http://ws.apache.org/axis2">
ZJHelloWorld!
</hw:sayHelloResponse>
 
4 .后续
详细介绍 client server 端代码。《 基于Tomcat5.0和Axis2开发Web Service代码详解

你可能感兴趣的:(tomcat,Web,service,axis2,休闲)