---
title: "webservice AXIS实现实例"
author: "heidian"
date: "2009-08-31T13:35:06Z"
tags: []
slug: "webservice-axis"
draft: false
lastmod: "2009-08-31T13:35:06Z"
id: 459225
url: "https://heidian.iteye.com/blog/459225"
---

1.环境：  
       JDK1.5  
       TOMCAT 5.5.23  
       MYECLIPSE6.6GA  
       Axis1.4

2.Axis 支持三种web service开发方式，分别为:

1 、Dynamic Invocation Interface ( DII)

2 、Dynamic Proxy方式

3 、Stubs方式  
(本实例只讨论 stubs 方式)

3\. 本实例工程axis\_example目录结构如下：

-axis\_example

    ----jws

    ----src

    ----WEB-INF

         ---classes

         ---lib

   目录说明如下：

   jws ：存放\*.jws文件

   src ：java源码

   WEB-INF/classes ：java编译后的class文件

   WEB-INF/lib ：需要用到的jar包

4.配置步骤：  
1.建立web project 工程，添加AXIS所需要的包。

在myeclipse 上建立一个web工程，并将

1).axis-bin-1.4\\axis-1\_4\\lib 下的所有包拷贝到axis\_example/WEB-INF/lib目录下，

2).axis-bin-1.4\\axis-1\_4\\webapps\\axis\\WEB-INF下的web.xml文件拷贝到axis\_example/WEB-INF目录下。

2.编写需要发布的服务类，并发布服务。  
\`\`1).在axis\_example/src下新建一MyServic.java文件，内容为:  
   public class MyService {  
        public String processService(String arg){  
            return arg;  
        }  
   }  
2).在src同级目录下建立deploy.wsdd。内容为：  
   &lt;deployment xmlns="http://xml.apache.org/axis/wsdd/"  
                xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"&gt;  
   &lt;service name="MyService" provider="java:RPC"&gt;  
      &lt;parameter name="className" value="MyService"/&gt;  
      &lt;parameter name="allowedMethods" value="processService"/&gt;  
   &lt;/service&gt;  
   &lt;/deployment&gt;  
3）部署web工程，启动tomcat  
4）在axis\_example/WEB-INF目录下执行：  
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis\_example/servlet/AxisServlet deploy.wsdd  
执行后可看到在axis\_example/WEB-INF目录下生成server-config.wsdd文件。  
5)重新启动tomcat ，以便加载 server-config.wsdd 文件。  
6）在src下创建 Client.java 内容为：  
   public static void main(String \[] args) throws Exception {  
        // 指出service所在URL  
        String endpoint = "http://localhost:" + "8080" + "/axis\_example/services/MyService";  
        // 创建一个服务(service)调用(call)  
        Service service = new Service();  
        Call call = (Call) service.createCall();// 通过service创建call对象  
        // 设置service所在URL  
        call.setTargetEndpointAddress(new java.net.URL(endpoint));  
        // 方法名(processService)与MyService.java方法名保持一致  
        call.setOperationName("processService");

        // Object 数组封装了参数，参数为"This is Test!",调用processService(String arg)  
        String ret = (String) call.invoke(new Object\[]{"This is Test!"});  
        System.out.println(ret);  
    }  
7） 编译Client.java，运行其中的main方法进行测试，可以看到屏幕打印出：" This is Dynamic Proxy test!"

3.使用WSDL2JAVA生成客户端使用的桩  
   axis 提供了wsdl2java工具，web service服务器端提供了一个地址，可以访问到WSDL文件，wsdl2java工具格式为：java org.apache.axis.wsdl.WSDL2Java \[options] WSDL-URI  
java -Djava.ext.dirs= E:\\project\\axis\_example\\WEB-INF\\lib org.apache.axis.wsdl.WSDL2Java http://localhost:8081/axis\_example/services/MyService?wsdl -p test.mytest -o E:\\project\\axis\_example\\src  
生成相应的客户端java文件。  
参数  
\-p   指定生成的java文件包名  
\-o   指定生成的java文件输出目录

如果不指定包名，axis会根据命令参数 WSDL-URI 生成相应的包名，如localhost\\axis\_example\\jws\\MyService\_jws

上述命令会在 E:\\project\\axis\_example\\src\\test\\mytest 目录下生成四个文件：  
MyServiceSoapBindingStub.java （相当于上面的MyService.java）  
MyService\_PortType.java （相当于上面的MyServiceInterface.java）  
MyServiceService.java/MyServiceServiceLocator.java （Service Locator模式，隐藏了具体的业务逻辑）

编写junit单元测试，在axis\_example\\src\\test\\mytest下新建一TestClient.java文件（拷贝junit.jar包到axis\_example/WEB-INF目录下），内容为：  
package test.mytest;  
import junit.framework.TestSuite;  
import junit.framework.TestCase;  
import junit.framework.Test;

public class TestClient extends TestCase {  
    public TestClient(String string) {  
        super(string);  
    }  
    public void MyServiceClient() throws Exception {  
        MyServiceService service = new MyServiceServiceLocator();  
        MyService\_PortType client = service.getMyService() ;  
        String ret = client.processService("This is Junit Test!");  
        System.out.println(ret);  
    }  
    public static Test suite() {  
        TestSuite suite = new TestSuite();  
        suite.addTest(new TestClient("MyServiceClient"));  
        return suite;  
    }  
}  
编译上面四个service文件，并编译运行 TestClient.java ，看到屏幕打印出：" This is Junit Test!"