您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 简单介绍基于AXIS实现WebService开发
简单介绍基于AXIS2实现WebService开发1.WebService介绍WebService是能够让一个程序可以透明地调用互联网的程序,不用关心具体的实现细节,只要其公开了服务接口,远程客户端就可以调用服务,它是基于http协议的组件服务,是分布式应用程序的发展趋势。2.Axis的介绍Axis是一款开源的WebService运行引擎,它是SOAP协议的一个实现,其本身来源于Apache的另一个项目ApacheSOAP,目前Axis2是比较流行的WebService引擎,它不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的RESTWebService,同时还支持Spring、JSON等技术。2.1Axis2的下载与安装可以从下载Axis2的最新版本,可以下载以下两个zip包axis2-1.5.2-bin.zipaxis2-1.5.2-war.zip其中axis2-1.5.2-bin.zip文件中包含了Axis2中所有的jar文件,axis2-1.5.2-war.zip文件用于将WebService发布到Web容器中。将axis2-1.5.2-war.zip、axis2-1.5.2-bin.zip文件解压到相应的目录,并将axis2-1.5.2-war目录中的axis2.war文件放到Tomcat安装目录\webapps目录中(本文使用的Tomcat的版本是6.x),并启动Tomcat。在安装完成后需要设置系统变量%AXIS2_HOME%,为其解压目录如:D:\nowuse\apache-tomcat-6.0.20\webapps\axis2,并且要将%AXIS2_HOME%\bin加入到系统变量%PATH%中。访问地址,如果能够看到如下界面,表明部署成功。2.2安装Axis2CodeGenerator和Axis2ServiceArchiver插件将下载好的插件安装文件Axis2_Codegen_Wizard_1.2.0.zip和Axis_Service_Archiver_1.0.0.zip解压到eclipse中的plugins目录中,重启动eclipse即可,若eclipse在File-New-Other-Axis2Wizards中看到如下图所示的插件就表示插件安装成功,如图示:3.用一个Webservice调用另一个Webservice代理WebService端ServiceAA的代码如下:此服务端代码主要实现对实际Webservice端的ServiceBB的空实现目的还是调用WebServiceClient。importjava.io.IOException;importorg.apache.axis2.AxisFault;importorg.apache.axis2.addressing.EndpointReference;publicclassServiceAA{ClientBBa=newClientBB();DataFormdf;Object[]temp;EndpointReferencetargetEPR=newEndpointReference();//上传图像,imageByte参数表示上传图像文件的字节publicbooleanuploadImageWithByte(byte[]imageByte,intlength)throwsAxisFault,IOException{a.sendFile(:\\images.jpg);returntrue;}//返回一维字符串数组publicString[]getArray()throwsAxisFault{return(String[])a.invokeMethod1();}//返回二维字符串数组publicString[]getMDArray()throwsAxisFault{return(String[])a.invokeMethod1();}//返回DataForm类的对象实例publicDataFormgetDataForm()throwsAxisFault{return(DataForm)a.invokeMethod2();}//将DataForm类的对象实例序列化,并返回序列化后的字节数组publicbyte[]getDataFormBytes()throwsAxisFault{df=(DataForm)a.invokeMethod2();java.io.ByteArrayOutputStreambaos=newjava.io.ByteArrayOutputStream();returnbaos.toByteArray();}//使用byte[]类型参数上传二进制文件publicbooleanuploadWithByte(byte[]file)throwsAxisFault,IOException{a.sendFile(:\\qq.docx);returntrue;}}实际WebService的ServiceBB的代码与ComplexServiceImpl类代码一样:主要是文件上传,返回数组,返回对象实例的具体实现。给代理WebService发送请求的WebServiceClient的ClientAA代码如下:主要是对发送请求给ServiceAA且实现对另一个WebServiceClient的方法调用并对客户端进行了测试。importjava.io.IOException;importorg.apache.axis2.AxisFault;importorg.apache.axis2.addressing.EndpointReference;importorg.apache.axis2.client.Options;importorg.apache.axis2.rpc.client.RPCServiceClient;publicclassClientAA{publicEndpointReferencetargetEPR=newEndpointReference();Optionsoptions;RPCServiceClientserviceClient;publicClientAA(){try{serviceClient=newRPCServiceClient();}catch(AxisFaulte){//TODOAuto-generatedcatchblocke.printStackTrace();}options=serviceClient.getOptions();options.setTo(targetEPR);}publicstaticvoidmain(String[]args){ClientBBa=newClientBB();ClientAAaa=newClientAA();try{//下面的代码调用uploadImageWithByte方法上传图像文件a.sendFile(:\\images.jpg);System.out.println(a.file.length());////下面的代码调用uploadWithByte方法上传二进制文件a.sendFile(:\\qq.docx);System.out.println(a.file.getName());//////下面的代码调用了getArray方法,并返回一维String数组Object[]temp=a.invokeMethod1();for(Objects:temp)System.out.print(s+);System.out.println();//////下面的代码调用了getMDArray方法,并返回一维String数组temp=a.invokeMethod1();for(Objects:temp){String[]array=((String)s).split(,);for(Stringss:array)System.out.print(+ss+);System.out.println();}//////下面的代码调用了getDataForm方法,并返回DataForm对象实例DataFormdf=(DataForm)a.invokeMethod2();System.out.println(df.getAge());System.out.println(df.getId());//////下面的代码调用了getDataFormBytes方法,并返回字节数组,最后将返回的字节数组反序列化后,转换成DataForm对象实例df=(DataForm)a.invokeMethod2();System.out.println(df.getName());}catch(AxisFaulte){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}给实际WebService发送请求的WebServiceClient的ClientBB的代码如下:主要是客户端的具体实现importjava.io.IOException;importjavax.xml.namespace.QName;importorg.apache.axis2.AxisFault;importorg.apache.axis2.addressing.EndpointReference;importorg.apache.axis2.client.Options;importorg.apache.axis2.rpc.client.RPCServiceClient;publicclassClientBB{RPCServiceClientserviceClient;Optionsoptions;QNameopAddEntry;java.io.Filefile;byte[]buffer;publicClientBB(){try{serviceClient=newRPCServiceClient();options=serviceClient.getOptions();}catch(AxisFaulte){e
本文标题:简单介绍基于AXIS实现WebService开发
链接地址:https://www.777doc.com/doc-2173475 .html