---
title: "AXIS入门实例"
author: "sineat"
date: "2007-06-06T18:27:08Z"
tags: []
slug: "axis"
draft: false
lastmod: "2007-06-06T18:27:08Z"
id: 87429
url: "https://sineat.iteye.com/blog/87429"
---

开发环境，windows2000、Eclipse3.0、Tomcat5.0、JDK1.4。  
    以下关于Axis的应用是在在基于上面所有环境已安装及配置成功的前提下。  
1、下载axis软件包，在此使用的是axis1.4版本，下载地址为：  
http://www.apache.org/dyn/closer.cgi/ws/axis/1\_4  
2、安装测试  
    解压缩软件包，如axis-bin-1\_4.zip，即可得到axis的应用文档及开发包。将  
...\\axis-bin-1\_4\\axis-1\_4\\webapps\\ 下的WEB应用axis目录置于Tomcat相应用目录，如  
…\\Tomcat 5.0\\webapps。启动tomcat服务，打开页面: [http://127.0.0.1:8080/axis/](http://127.0.0.1:8080/axis/)，如果看到以下页面出来，测试成功。反之，请检查环境配置。

![](http://img.photo.163.com/iIwIfChnnu_tKfrguyPFmA==/162129586588352589.jpg)

 

 3、axis初步应用  
    下载另外两个开发应用包，activation.jar和mail.jar。  
接下来就是如何利用开发包进行服务的发布，关于服务的发布有即时发布和定制发布，以下分别举例说明。  
3.1 即时发布  
    在eclipse中新建java或tomcat项目（配置过lomboz插件），引入外部库文件，即：axis-bin-1\_4\\axis-1\_4\\lib下所有jar包，再引入activation.jar和mail.jar，这样环境即搭建好了，新建一个JWS文件，如GetHexString.jws。内容如下：  
*public class GetHexString {*  
 *public static String getHexString(String str){*  
  *if ((str==null)||(str=="")) return null;*  
  *byte strBytes\[]=str.getBytes();*  
  *int index=0;*  
  *int len=strBytes.length;*  
  *return (byteToHexstring(strBytes,0,len));*  
 *}*  
 */\** 十六进制字节的相应的字符串\** \*/*  
 *public static String byteToHexstring(byte\[] sb,int index,int len) {*  
  *char hexDigit\[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',*  
    *'A', 'B', 'C', 'D', 'E', 'F' };*  
  *String result="";*  
  *//char\[] array={0x30,0x30}; ;*  
  *for (int i=index;i    char\[] array = { hexDigit\[(sb\[i] &gt;&gt; 4) & 0x0f], hexDigit\[sb\[i] &*

*0x0f] };*  
   *result+=new String(array)+" ";*  
  *}*    
  *return result;*  
 *}*   
*}*  
    将GetHexString.jws 复制到…\\Tomcat 5.0\\webapps\\axis下，重启Tomcat服务，打开页面  
http://127.0.0.1:8080/axis/GetHexString.jws?wsdl  
    得到描述代码如下  
&lt;wsdl:service name="GetHexStringService"&gt;＜？xml version="1.0" encoding="UTF-8" ?＞  
……  
＜wsdl:service name="GetHexStringService"＞  
 ＜wsdl:port binding="impl:GetHexStringSoapBinding" name="GetHexString"＞  
  ＜wsdlsoap:address location="http://127.0.0.1:8080/axis/GetHexString.jws" /＞  
  ＜/wsdl:port＞  
  ＜/wsdl:service＞  
……  
＜/wsdl:definitions＞&lt;/wsdl:service&gt;

3.2 定制发布  
3.2.1创建服务程序，即新建一个java类，内容如下：  
*public class FixedString {*  
 *public static String fixedLen(String para, int len) {*  
  *String result = "" + para;*  
  *int n = len - result.length();*  
  *for (int i = 0; i &lt; n; i++) {*  
   *result = "0" + result;*  
  *}*  
  *return result;*  
 *}*  
*}*  
    将编译后的class类放在…\\Tomcat 5.0\\webapps\\axis\\WEB-INF\\classes\\samples下。  
3.2.2 编写wsdd（服务发布描述文件deploy.wsdd（Web Service Deployment Descriptor的缩写），这个文件负责描述服务的名称、入口等信息，服务描述内容如下：  
 &lt;service name="FixedString" provider="java:RPC" use="encoded" type=""&gt;&lt;/service&gt;  
   ＜service name="FixedString" type="" provider="java:RPC" style="rpc" use="encoded"＞  
        ＜parameter name="scope" value="Request"/＞  
        ＜parameter name="className" value="samples.FixedString"/＞  
        ＜parameter name="allowedMethods" value="\*"/＞  
        ＜namespace＞http://test1.sineat.www＜/namespace＞  
        ＜typeMapping  
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"  
            qname="ns1:ArrayOfint" languageSpecificType="java:int\[]"  
            serializer="org.apache.axis.encoding.ser.ArraySerializerFactory"  
            deserializer="org.apache.axis.encoding.ser.ArrayDeserializerFactory"  
            name="ArrayOfint" xmlns:ns1="http://test1.sineat.www"/＞  
    ＜/service＞        
        
     “java:RPC”： 服务的提供者，内建在Axis中，指明了一个JAVA RPC服务，处理的类是               org.apache.axis.providers.java.RPCProvider；  
        "samples.FixedString"：远程所用到的服务类，包路径及类名的全称；  
http://test1.sineat.www：客户端程序产生在test1.sineat.www下。  
        将以上服务内容取代原server-config.wsdd中相应内容，server-config.wsdd全文参见附录说明。文件位于…\\Tomcat 5.0\\webapps\\axis\\WEB-INF下。  
测试页面地址：http://127.0.0.1:8080/axis/services/FixedString?wsdl  
3.2.3 客户端程序开发：  
    运行程序  
    main类设为：org.apache.axis.wsdl.WSDL2Java；  
    自变量参数：[http://127.0.0.1:8080/axis/services/FixedString?wsdl](http://127.0.0.1:8080/axis/services/FixedString?wsdl) 。  
    在项目为将产生以下4个类，结构图如下。  
 &lt;v:shapetype id="\_x0000\_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" filled="f" stroked="f" coordsize="21600,21600" o:spt="75"&gt; ![](http://img.photo.163.com/P93v_zx4jBdd5QkW49YCeg==/168322036073290019.jpg)&lt;v:stroke joinstyle="miter"&gt;&lt;/v:stroke&gt;&lt;v:formulas&gt;&lt;v:f eqn="if lineDrawn pixelLineWidth 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 1 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum 0 0 @1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @2 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 0 1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @6 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @8 21600 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @10 21600 0"&gt;&lt;/v:f&gt;&lt;/v:formulas&gt;&lt;v:path o:connecttype="rect" o:extrusionok="f" gradientshapeok="t"&gt;&lt;/v:path&gt;&lt;o:lock aspectratio="t" v:ext="edit"&gt;&lt;/o:lock&gt;&lt;/v:shapetype&gt;  
    假设服务程序类名为xxx，则相应的4个类为xxx..java、xxxService、xxxServiceLocator和xxxSoapBindingStub。  
    调用服务的方法  
    新建类，如TestService，内容如下：  
*public class TestService {*  
 *public static void main(String\[] args) throws Exception {*  
  *FixedStringService ssc = new FixedStringServiceLocator();*  
  *try {*  
   *FixedString fs = ssc.getFixedString();*  
   *String str = fs.fixedLen("abc", 10);*  
   *System.err.println("str=" + str);*  
  *} catch (ServiceException e) {*  
   *// TODO 自动生成 catch 块*  
   *e.printStackTrace();*  
  *}*  
 *}*  
*}*  
    运行程序如果：str=0000000abc  
4、服务的get方式访问  
写一jws文件，内容如下。  
*public class PrintDemo{*  
    *public String print() {*  
  *System.out.println("Webservice display testing by Zhao.Jian");*  
*//在服务器端打印输出*  
  *return "Webservice display testing by Zhao.Jian!";//返回相应字符串*  
 *}*  
*}*   
    将文件置于…\\Tomcat 5.0\\webapps\\axis 目录下，访问页面：  
http://127.0.0.1:8080/axis/PrintDemo.jws?method=print   
    返回的SOAP信封消息为：&lt;soapenv:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;soapenv:body&gt;   
＜soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"＞  
＜soapenv:Body＞  
＜printResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"＞  
  ＜printReturn xsi:type="xsd:string"＞Webservice display testing by Zhao.Jian!＜/printReturn＞  
  ＜/printResponse＞  
  ＜/soapenv:Body＞  
  ＜/soapenv:Envelope＞  
&lt;/soapenv:body&gt;&lt;/soapenv:envelope&gt;5、客户端的服务访问编程  
    关于Axis实现SOAP的API请参阅…\\axis-bin-1\_4\\axis-1\_4\\docs\\index.html。  
org.apache.axis.client.Call和org.apache.axis.client.Service是两个比较常用的类，一般的客户端程序要访问一个Web Service时，都要生成一个客户端的Service对象和Call对象，在访问服务之前，首先要对Call对象设置相应的参数，包括服务的位置、操作名、入口参数、返回值类型等，最后调用Call对象的invoke方法访问服务。  
以下是一个客户端访问服务的程序实例，包括2种发布方式的服务调用的实现方法。  
文件AxisAccess.java原文。  
*package test.sineat.www;*  
*import org.apache.axis.client.Call;*  
*import org.apache.axis.client.Service;*  
*import org.apache.axis.encoding.XMLType;*  
*import javax.xml.rpc.ParameterMode;*  
*public class AxisAccess {*  
 *public static void main(String\[] args) throws Exception {*  
  *//创建service和call对象，标准的JAX-RPC对象，用于存储服务调用的数据（metadata）。*  
  *Service service = new Service();*  
  *Call call = (Call) service.createCall();//访问即时发布的Distance服务*  
  *call.setTargetEndpointAddress("http://127.0.0.1:8080/axis/GetHexString.jws");//设置访问点*  
  *call.setOperationName("getHexString");//设置操作名*  
  *call.addParameter("op1", XMLType.XSD\_STRING, ParameterMode.IN);//设置入口参数：字符型，入口*  
  *//设置返回参数类型*  
  *call.setReturnType(XMLType.XSD\_STRING);//返回类型：字符*  
  *String para1 = "123abc";*  
  *//调用服务，在invoke方法中传入的是包含调用参数的数组*  
  *System.out.println("字符串:" + para1 + "对应的字节="*  
    *+ call.invoke(new Object\[] { para1 }) + " ！");*  
  *call = (Call) service.createCall();//访问定制发布的FixedString服务*  
*call.setTargetEndpointAddress("http://127.0.0.1:8080/axis/services/FixedString");//设置访问点*  
*call.setOperationName("fixedLen");//设置操作名*  
 *call.addParameter("op1", XMLType.XSD\_STRING, ParameterMode.IN);//设置入口参数：字符型，入口*  
 *call.addParameter("op2", XMLType.XSD\_INT, ParameterMode.IN);//添加入口参数:*   
   *// 整型,入口*  
  *call.setReturnType(XMLType.XSD\_STRING);//返回类型 字符*  
  *para1 = "123";*  
  *Integer para2 = new Integer(10);*  
  *//调用服务*  
  *System.out.println("字符串:" + para2 + "经处理后= "*  
    *+ call.invoke(new Object\[] { para1, para2 }));*  
 *}*  
*}*  
运行结果：  
字符串:123abc对应的字节=31 32 33 61 62 63  ！  
字符串:10经处理后= 0000000123

特殊说明：为了正常显示效果，XML文件中'&lt;' 由'＜'代替，'?'由'？'代替。