RPC(RPCServiceClient)方式调用webservice SOAP调用

/**
 * 应用rpc的方式调用 这种方式就等于远程调用,
 * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。
 * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService
 *
 【注】:
 
 如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数
 第一个参数的类型是QName对象,表示要调用的方法名;
 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
 第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。
 
 如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法
 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。
 
 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,
 也就是 <wsdl:definitions>元素的targetNamespace属性值。
 *
 */
public String webserviceAxis2(String methodName,LinkedHashMap param) {
   log.info("调用方法:"+methodName+",输入参数:"+JSON.toJSONString(param));
   String result = "";
   // 使用RPC方式调用WebService
   RPCServiceClient serviceClient = null;
   try {
      // axis2 服务端地址
      String htWsUrl = htDomain+"OAapp/WebObjects/OAapp.woa/ws/WSClientInterface";
      serviceClient = new RPCServiceClient();
 
      // 指定调用WebService的URL
      EndpointReference targetEPR = new EndpointReference(htWsUrl);
 
      /*MessageContext msgContext = new MessageContext();
      msgContext.setServiceContext(serviceClient.getServiceContext());
      MessageContext.setCurrentMessageContext(msgContext);*/
 
      Options options = serviceClient.getOptions();
 
      //确定目标服务地址
      options.setTo(targetEPR);
      //确定调用方法
      options.setAction("urn:"+methodName);
      options.setProperty(HTTPConstants.CHUNKED, "false");// 把chunk关掉后,会自动加上Content-Length
      //解决高并发链接超时问题
      options.setManageSession(true);
      options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);
 
      //设置响应超时,默认5s
      options.setProperty(HTTPConstants.SO_TIMEOUT, TIMEOUT_MILLISECONDS);
      //设置连接超时,默认5s
      options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, TIMEOUT_MILLISECONDS);
 
      //options.setTimeOutInMilliSeconds(TIMEOUT_MILLISECONDS_AXIS2);
      /**
       * 指定要调用的getPrice方法及WSDL文件的命名空间
       * 如果 webservice 服务端由axis2编写
       * 命名空间 不一致导致的问题
       * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0
       */
      QName qname = new QName("http://xml.apache.org/axis/wsdd/", methodName);
 
      // 指定传递参数值
      //Object[] parameters = new Object[] { "18911215691","ROOT" };
      Object[] parameters = new Object[param.size()];
 
      // 设置参数名称,具体参照从浏览器中看到的
      if(param!=null&&!param.isEmpty()){
         Set set = param.entrySet();
         Iterator it = set.iterator();
         int i=0;
         while(it.hasNext()){
            Entry entry = (Entry)it.next();
            String key = entry.getKey().toString();
 
            if(entry.getValue() instanceof byte[]){//把object转换成字节数组
               parameters[i]=toByteArray(entry.getValue());
            }else{
               parameters[i]=entry.getValue();
            }
 
            i++;
         }
      }
 
      // 指定方法返回值的数据类型的Class对象
      Class[] returnTypes = new Class[] { String.class };
 
      // 调用方法并输出该方法的返回值
      Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);
      result = (String) response[0];
 
      log.info("webserviceAxis2 result:"+result);
   } catch (org.apache.axis2.AxisFault e) {
      log.error("第三方接口异常",e);
      if(e.getCause()!=null&&e.getCause().toString().toLowerCase().contains("timeout")){
         throw new BusinessException("第三方服务连接超时,请稍候重试!","WB_301");
      }
      throw new BusinessException("第三方服务异常,请稍候重试!","WB_301");
   } finally {
      try {
         if(serviceClient != null){
            serviceClient.cleanupTransport();
         }
      } catch (org.apache.axis2.AxisFault e) {
         log.error("第三方接口异常 finally",e);
         if(e.getCause()!=null&&e.getCause().toString().contains("SocketTimeoutException")){
            throw new BusinessException("第三方服务连接超时,请稍候重试!","WB_301");
         }
         throw new BusinessException("第三方服务异常,请稍候重试!","WB_301");
      }
   }
 
   return result;
}

 

public static void main(String[] args) {
   try {
      //测试
      LinkedHashMap map = new LinkedHashMap();
      map.put("phone", "189000000");
      map.put("parentId", "ROOT");
      String result = new 类名.webserviceAxis2("getDMagazineDirList",map);
   } catch (Exception e) {
      e.printStackTrace();
   }
}

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个搭建能生成webservice的demo的示例。以下是基于Java语言,使用Eclipse IDE和Apache Axis2框架实现的示例: 1. 下载并安装Eclipse IDE 2. 安装Apache Axis2框架插件 在Eclipse IDE中,选择"Help" -> "Eclipse Marketplace",搜索"Axis2",安装"Apache Axis2 Tools for Eclipse"插件。 3. 创建Java项目 在Eclipse IDE中,选择"File" -> "New" -> "Java Project",输入项目名称,点击"Finish"。 4. 创建Java类 在Java项目中,创建一个Java类,例如"HelloWorld.java",添加以下内容: ```java package com.example; public class HelloWorld { public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 5. 生成webservice 在Eclipse IDE中,右键点击"HelloWorld.java"文件,选择"Web Services" -> "Create Web Service"。 在弹出的对话框中,选择"Axis2 Service",点击"Next"。 选择"Generate the Service Implementation",点击"Next"。 在"Service Implementation"页面,选择"HelloWorld"类,点击"Next"。 在"Methods"页面,选择"sayHello"方法,点击"Next"。 在"Service Definition"页面,点击"Finish"。 6. 启动webservice 在Eclipse IDE中,右键点击"HelloWorldService.aar"文件,选择"Web Services" -> "Start Web Service". 7. 调用webservice 在Java项目中,创建一个Java类,例如"Client.java",添加以下内容: ```java package com.example; import java.rmi.RemoteException; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class Client { public static void main(String[] args) throws AxisFault, RemoteException { RPCServiceClient client = new RPCServiceClient(); Options options = client.getOptions(); EndpointReference address = new EndpointReference("http://localhost:8080/axis2/services/HelloWorldService"); options.setTo(address); Object[] params = new Object[] { "World" }; Class<?>[] returnTypes = new Class[] { String.class }; Object[] response = client.invokeBlocking("sayHello", params, returnTypes); String result = (String) response[0]; System.out.println(result); client.cleanupTransport(); } } ``` 8. 运行客户端 在Eclipse IDE中,右键点击"Client.java"文件,选择"Run As" -> "Java Application",即可看到输出结果:"Hello World!"。 希望这个示例能够帮助到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值