axis2 java.net.url_axis2调用.net写的webservice接口实现,指定参数名

参考文章:https://blog.csdn.net/wangyu2016/article/details/76022928

使用axis2调用调用.net写的webservice接口时出现参数无法传递给接口的问题,找了很多资料,一下是找到可以实现的方法

正确代码为:

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;

import java.rmi.RemoteException;

public class Axis2Test

{

public static void main(String[] args) throws RemoteException {

test();

}

public static void test() throws AxisFault {

try {

String url = "目标URL";

Options options = new Options();

EndpointReference targetEPR = new EndpointReference(url);

options.setTo(targetEPR);

options.setAction("目标的TargetNameSpace"+"调用的方法名");//需要加上这条语句

ServiceClient sender = new ServiceClient();

sender.setOptions(options);

OMFactory fac = OMAbstractFactory.getOMFactory();

String tns = "目标的TargetNameSpace";

OMNamespace omNs = fac.createOMNamespace(tns, "");

OMElement method = fac.createOMElement("调用的方法名", omNs);

OMElement symbol = fac.createOMElement("参数名", omNs);

symbol.addChild(fac.createOMText(symbol, "参数值"));

method.addChild(symbol);

method.build();

OMElement result = sender.sendReceive(method);

System.out.println(result);

} catch (AxisFault axisFault) {

axisFault.printStackTrace();

}

}

}

详细步骤:

第一步    添加axis2相关的jar包,我使用maven管理jar包

maven   中pom.xml配置如下:

1.3

1.2.5

1.1.1

1.6.2

1.4.5

3.1

3.0

backport-util-concurrent

backport-util-concurrent

${backport-util-concurrent.version}

commons-httpclient

commons-httpclient

${commons-httpclient.version}

org.apache.ws.commons.schema

XmlSchema

${XmlSchema.version}

wsdl4j

wsdl4j

${wsdl4j.version}

commons-logging

commons-logging

${commons-logging.version}

org.apache.ws.commons.axiom

axiom

${axiom.version}

org.apache.axis2

axis2

${axis2.version}

第二步:使用axis方法调用webservice接口

/**

* 测试 退费处理

* @return

*/

public static void refundHandle_test2() {

try {

//读取配置文件中的ur、命名空间、方法名

PropertiesUtil propUtil = new PropertiesUtil("RefundInf.properties");

Properties pros = propUtil.getProperties();

String webServiceURL = pros.getProperty("webServiceURL");

String actionStr = pros.getProperty("actionStr");

String actionName = pros.getProperty("actionName");

String url = webServiceURL;

Options options = new Options();

EndpointReference targetEPR = new EndpointReference(url);

options.setTo(targetEPR);

options.setAction(actionStr+actionName);//需要加上这条语句

ServiceClient sender = new ServiceClient();

sender.setOptions(options);

OMFactory fac = OMAbstractFactory.getOMFactory();

String tns = actionStr;

OMNamespace omNs = fac.createOMNamespace(tns, "");

OMElement method = fac.createOMElement(actionName, omNs);

OMElement symbol = fac.createOMElement("json", omNs);

//json字符串参数组装

JsonObject obj = new JsonObject();

obj.addProperty("OrgBillNo", "201806150939108");

obj.addProperty("transaction_id", "");

obj.addProperty("HisRefundNo", "2018061510081109");

obj.addProperty("total_fee", "0.10");

obj.addProperty("Amount", "0.01");

obj.addProperty("payChannel", "zfb");

String aaa = obj.toString();

symbol.addChild(fac.createOMText(symbol,aaa));//为指定的参数名传入参数值

method.addChild(symbol);

method.build();

OMElement result = sender.sendReceive(method);

System.out.println(result);

} catch (AxisFault axisFault) {

axisFault.printStackTrace();

}

}

这样就可以把参数传递给.net的webservice服务了,,正确获取接口返回信息

另外备注一下,完成这个任务后,我会尝试用axis1和HttpClient试试调用接口的方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值