axis2实现web service(二)




上一篇,记录了通过axis2创建发布web service服务,这一篇将记录,如何通过 axis2调用web service 接口,目前找到两种方法,这一篇先介绍其中一种方法,下一篇介绍另一种方法:

 

通过 axis2调用web service方法一:不采用stub方式

1、新建一个java project,将 axis2/lib 下所有的包都添加进来(由于包较多,可以建一个 user library 将这些包放一起 )

2、建包,建类,源码如下:

[java]  view plain copy
  1. package wsclient;  
  2.   
  3. import javax.xml.namespace.QName;  
  4.   
  5. import org.apache.axis2.AxisFault;  
  6. import org.apache.axis2.addressing.EndpointReference;  
  7. import org.apache.axis2.client.Options;  
  8. import org.apache.axis2.rpc.client.RPCServiceClient;  
  9.   
  10.   
  11.   
  12. public class HelloClient {  
  13.     private RPCServiceClient serviceClient;  
  14.     private Options options;  
  15.     private EndpointReference targetEPR;  
  16.   
  17.     public HelloClient(String endpoint) throws AxisFault {  
  18.        serviceClient = new RPCServiceClient();  
  19.        options = serviceClient.getOptions();  
  20.        targetEPR = new EndpointReference(endpoint);  
  21.        options.setTo(targetEPR);  
  22. }  
  23.       
  24.     public Object[] invokeOp(String targetNamespace, String opName,Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,ClassNotFoundException {  
  25.         // 设定操作的名称  
  26.         QName opQName = new QName(targetNamespace, opName);  
  27.         // 设定返回值  
  28.   
  29.         // Class<?>[] opReturn = new Class[] { opReturnType };  
  30.   
  31.         // 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用  
  32.         return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);  
  33.      }  
  34.   
  35.   
  36.      public static void main(String[] args) throws AxisFault,  
  37.          ClassNotFoundException {  
  38.          // TODO Auto-generated method stub  
  39.          final String endPointReference = "http://localhost:8080/axis2/services/Hello";  
  40.          final String targetNamespace = "http://ws";  
  41.          HelloClient client = new HelloClient(endPointReference);  
  42.   
  43.          String opName = "sayHello";  
  44.          Object[] opArgs = new Object[] { "tester" };  
  45.          Class<?>[] opReturnType = new Class[] { String[].class };  
  46.   
  47.         Object[] response = client.invokeOp(targetNamespace, opName, opArgs,opReturnType);  
  48.         System.out.println(((String[]) response[0])[0]);  
  49.      }  
  50.   
  51.  }  

 

3、运行,打印出:Hello,tester

 

则说明调用成功!

 

注:上述代码来自:http://blog.sina.com.cn/s/blog_5cef6d430100jr4f.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值