`
sineat
  • 浏览: 20328 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

AXIS入门实例

阅读更多

开发环境,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/,如果看到以下页面出来,测试成功。反之,请检查环境配置。

 

 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] >> 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
    得到描述代码如下
<wsdl:service name="GetHexStringService"><?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></wsdl:service>

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 < 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的缩写),这个文件负责描述服务的名称、入口等信息,服务描述内容如下:
 <service name="FixedString" provider="java:RPC" use="encoded" type=""></service>
   <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
    在项目为将产生以下4个类,结构图如下。
 <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"> <v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" o:extrusionok="f" gradientshapeok="t"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype>
    假设服务程序类名为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信封消息为:<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"><soapenv:body> 
<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>
</soapenv:body></soapenv:envelope>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文件中'<' 由'<'代替,'?'由'?'代替。

分享到:
评论

相关推荐

    Axis实例,Axis

    Axis实例Axis入门Axis实例与分析详解

    Axis WebService 实例源码

    Tomcat+Axis+WebService 入门实例,讲述如何实现WebService的传递信息,

    SOAP与AXIS2入门教程附带实例

    SOAP与AXIS2入门教程附带实例,通过实例带您入门

    Apache Web Services Axis2(1.4.1) 详细经典入门实例图文并茂

    Apache Web Services Axis2(1.4.1) 详细经典入门实例,使用Eclipse Axis Plug-in,图文并茂,一看就明。

    axis2入门实例

    NULL 博文链接:https://spacecity.iteye.com/blog/1924945

    axis入门到深入及完全部署手册

    axis使得webservice相当简单,半天就可以精通

    AXIS2入门+进阶+webservice实例

    AXIS2入门+进阶+webservice实例。共3个文档。

    Axis2实用教程(入门教程包含实例)

    很好的axis2教程,有实例。本课程重点讲解了Axis2的核心功能,并提供了三个项目以使学员理解并掌握如何将Axis2应用于不同的场景。本课程站在初学者角度介绍了Axis2的大多数常用的功能

    WebService入门简单实例(采用AXIS的方式)

    NULL 博文链接:https://qiangjiyi.iteye.com/blog/2223655

    WebService大讲堂之Axis2,多个实例

    WebService大讲堂之Axis2,多个实例,非常全的文档,从新手入门开始到复杂的实例

    Axis2 WebService 开发指南 技术文档 入门文档

    Axis的入门实例 Axis复杂对象类型的WebService Axis的辅助工具发布、调用WebService Axis WebService会话Session的管理 Axis用控制台Dos命令发布WebService Axis跨多个WebService管理Session Axis用Spring的...

    axis2开发Web Services入门

    用axis2开发Web Services,内容包括环境搭建,插件安装,以及用axis2开发Web Services的详细步骤及实例程序;

    axis2;WebService

    Axis的入门实例 Axis复杂对象类型的WebService Axis的辅助工具发布、调用WebService Axis WebService会话Session的管理 Axis用控制台Dos命令发布WebService Axis跨多个WebService管理Session Axis用Spring的JavaBean...

    axis学习资料汇总

    axis学习资料汇总: Axis实例与分析详解; Axis学习笔记.pdf; Tomcat5.0.28下AXIS完全安装手册.doc; Axis1.4 开发指南_V1.0.pdf; AXIS入门及应用.rar; Axis开发Web+Services.doc 等等

    axis2_WebService_开发指南

    Axis 的入门实例 Axis 复杂对象类型的WebService Axis 的辅助工具发布、调用WebService Axis WebService 会话Session 的管理 Axis 用控制台Dos 命令发布WebService Axis 跨多个WebService 管理Session Axis 用Spring...

    Axis WebService 技术指南,开发文档

    Axis的简单入门实例 Axis的jws发布WebService Axis的wsdd模式发布WebService Axis中传递复杂对象和内部类 Axis中的handler、chain的特性 Axis中处理异常信息 Axis中传递文件信息 Axis常用命令介绍 Axis 提供的工具和...

    Axis_API实例教程打包

    axsis1.4的 API文档,chm格式,自己打包做的, 还有一本网络上收集的入门电子书,很详细。如果你准备用Axis开发的话应该很有用~有问题的话给我留言

    axis2_webservice

    Axis的入门实例 Axis复杂对象类型的WebService Axis的辅助工具发布、调用WebService AxisWebService会话Session的管理 Axis用控制台Dos命令发布WebService Axis跨多个WebService管理Session Axis用Spring的JavaBean...

Global site tag (gtag.js) - Google Analytics