微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService学习之Axis2

参考自:http://hi.baidu.com/niuhea/item/93cc23cd0de746dbef183b65

代码见:http://pan.baidu.com/share/link?shareid=1539706899&uk=1763236251

一、直接使用Axis.war发布WebService

              1、Axis2(axis2-1.5.5-war.zip)下载:http://ws.apache.org/axis2/将解压缩后的axis2.war拷贝到tomcat的webapps目录,部署Axis2项目。

              2、访问如下地址,查看已发布服务:http://localhost:8080/axis2/services/listServices

              3、编写发布自己的WebService(一个简单的POJO,其中所有public方法都将发布成WebService方法):

                   
packagezxn.webservice.shengxiao.impl;

 /**

 * 根据出生日期,计算属相

 */

publicclass ShengxiaoService {

    public StringgetShengxiao(int age) {

       // 计算生肖

       String str[]= { "猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊" };

       String res = "您属" + str[(age) % 12];

       return res;

    }
}

           

            4、通过javac编译上述文件(包名要不要无所谓),生成字节码文件

           

将class文件复制到部署的项目Axis2的WEB-INF/POJO(必须叫POJO,大小写无所谓)目录中。

 


             5、访问地址:http://localhost:8080/axis2/services/listServices,查看服务发布情况


            可能有错误,则 点击错误链接,查看错误内容

           


           该错误主要是Axis2 1.5.5版本使用jdk1.5(其meta-inf/MANIFEST.MF中可看到),而我编译类使用的是JDK1.7,高版本应用发布到低版本的项目中,导致版本出错。

           解决:下载最新Axis2,或者跟换本地jdk版本。

         6、重新刷新http://localhost:8080/axis2/services/listServices ,即可看到成功发布的服务:

          


        访问:http://localhost:8080/axis2/services/ShengxiaoService/getAge?args0=1990,看有无返回结果:

        


二、客户端动态调用方式访问WebService服务

              1、建立Java或者Java Web Project:MyFirstAxis2WebServiceClient

              2、将D:\programfiles\apache-tomcat-6.0.35\webapps\axis2\WEB-INF\lib(Axis2项目lib目录)下所有的jar包拷贝到项目中,并build to path

              3、建立测试类:

                    

packagezxn.webservice.shengxiao.test;

importjavax.xml.namespace.QName;

importorg.apache.axis2.AxisFault;

importorg.apache.axis2.addressing.EndpointReference;

importorg.apache.axis2.client.Options;

importorg.apache.axis2.rpc.client.RPCServiceClient;

publicclass TestFirstAxis2WebService{

   

    @SuppressWarnings("unchecked")

    publicstaticvoid main(String[] args) throws AxisFault {

       // 使用RPC 方式调用WebService

       RPCServiceClientserviceClient = new RPCServiceClient();

       Optionsoptions = serviceClient.getoptions();

       // 指定调用WebService 的URL

       EndpointReferenceendpointReference = new EndpointReference("http://localhost:8080/axis2/services/ShengxiaoService");

       options.setTo(endpointReference);

       // 指定getShengxiao、getAge 方法的参数值

       Object[]inArgs = new Object[] { 1990 };

       // 指定getShengxiao、getAge 方法返回值的数据类型的Class 对象

       Class[]outArgsTypeClasses = new Class[] { String.class };

       // 指定要调用的getShengxiao 方法及WSDL 文件的命名空间

       QName qName =new QName("http://ws.apache.org/axis2","getShengxiao");

       // 调用getGreeting 方法输出方法的返回值

       System.out.println(serviceClient.invokeBlocking(qName,inArgs,outArgsTypeClasses)[0]);

       // 下面是调用getAge 方法代码,这些代码调用getAge 方法代码类似

       outArgsTypeClasses= new Class[] { String.class };

       qName = new QName("http://ws.apache.org/axis2","getAge");

       System.out.println(serviceClient.invokeBlocking(qName,new Object[] {inArgs},outArgsTypeClasses)[0]);

    }

}

                  

               执行结果:

             



三、根据WSDL生成本地客户端方式访问WebService服务

              1、下载axis2-1.6.2-bin.zip,将其解压,如下(位置随意放)

             


             2、新建脚本文件genClientFiles.bat,放在src位置下:

             
:axis2-1.6.2-bin的解压缩目录

set AXIS2_HOME=D:\programfiles\axis2-1.6.2-bin\axis2-1.6.2

set JAVA_HOME=D:\programfiles\jdk1.6.0.24

:WSDL的路径地址,可以事先下载到本地

setSERVICE_WSDL_URI=http://localhost:8080/axis2/services/ShengxiaoService?wsdl

:设置输出路径

setoutput_Path=D:\ProBag\ChuanZhiRuanjian\MyEclipese10\projects\MyFirstAxis2WebServiceClient\

:设置包名

set Package=zxn.webservice.shengxiao.test

:不能换行

%AXIS2_HOME%\bin\wsdl2java -uri %sERVICE_WSDL_URI% -p %Package% -s -o%Output_Path%

pause


                  执行界面:

                

               生成文件如下:

              



              3、在测试类TestFirstAxis2WebService中写测试方法,用Junit测试:

             

 @Test
 publicvoid TestClient() throws remoteexception{

       ShengxiaoServiceStubservice = new ShengxiaoServiceStub();

       GetAge getAge= new GetAge();

       getAge.setArgs0(1990);

       GetAgeResponsegetAgeResponse = service.getAge(getAge);

        System.out.println(getAgeResponse.get_return());

    }
               结果如下:
              

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐