axis2客户端工具类

自己根据网上找的一些资料,写的一个调用axis2接口的客户端代码

首先依赖的包

<!-- axis2 -->
		<dependency>
			<groupId>org.apache.ws.commons.axiom</groupId>
			<artifactId>axiom-api</artifactId>
			<version>1.2.7</version>
		</dependency>
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-kernel</artifactId>
			<version>1.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-adb</artifactId>
			<version>1.4.1</version>
		</dependency>



代码

/**
 * 
 */
package com.common.utils;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

/**
 * axis2客户端工具类
 * @author luolin
 *
 * @version $id:Axis2ClientUtils.java,v 0.1 2015年11月26日 下午3:10:50 luolin Exp $
 */
public class Axis2ClientUtils {

    /**
     * 调用axis2接口
     * @param url wsdl地址
     * @param targetNamespace 命名空间
     * @param method 目标方法
     * @param args 参数集合,必须和目标接口的参数顺讯相同
     * @return 调用结果
     * @throws AxisFault
     */
    public static String callAxis2Server(String url, String targetNamespace, String method, Object[] args)
                                                                                                          throws AxisFault {
        Options options = new Options();
        // 指定调用WebService的URL  
        EndpointReference targetEPR = new EndpointReference(url);
        options.setTo(targetEPR);

        ServiceClient sender = new ServiceClient();
        sender.setOptions(options);

        OMFactory fac = OMAbstractFactory.getOMFactory();
        // 命名空间
        OMNamespace omNs = fac.createOMNamespace(targetNamespace, "");

        OMElement methodElement = fac.createOMElement(method, omNs);
        // 为方法添加参数,createOMElement方法的第一个参数可以不和目标接口的入参形参名相同
        for (Object obj : args) {
            OMElement param = fac.createOMElement("param", omNs);
            param.setText(String.valueOf(obj));
            methodElement.addChild(param);
        }

        methodElement.build();
        OMElement result = sender.sendReceive(methodElement);
        return result.getFirstElement().getText();
    }

}



转载于:https://my.oschina.net/simpleton/blog/542008

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值