博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
axis2调用.net写的webservice接口实现,指定参数名
阅读量:5131 次
发布时间:2019-06-13

本文共 5128 字,大约阅读时间需要 17 分钟。

 

参考文章: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配置如下:

<properties>

<!-- axis2 begin -->

<axis2.version>1.3</axis2.version>

<axiom.version>1.2.5</axiom.version>
 <commons-logging.version>1.1.1</commons-logging.version> 
<wsdl4j.version>1.6.2</wsdl4j.version>
<XmlSchema.version>1.4.5</XmlSchema.version>
<commons-httpclient.version>3.1</commons-httpclient.version>
<backport-util-concurrent.version>3.0</backport-util-concurrent.version>
<!-- axis2 end -->

</properties>

</dependencies>

<!-- axis2 begin -->

<!-- https://mvnrepository.com/artifact/backport-util-concurrent/backport-util-concurrent -->
<dependency>
<groupId>backport-util-concurrent</groupId>
<artifactId>backport-util-concurrent</artifactId>
<version>${backport-util-concurrent.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${commons-httpclient.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ws.commons.schema/XmlSchema -->
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>${XmlSchema.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>${wsdl4j.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
 <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ws.commons.axiom/axiom -->
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom</artifactId>
<version>${axiom.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2 -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>${axis2.version}</version>
</dependency>
<!-- axis2 end -->

</dependencies>

第二步:使用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试试调用接口的方法

转载于:https://www.cnblogs.com/bzsz-quan/p/9381766.html

你可能感兴趣的文章
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JAVA开发环境搭建
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>
SDN第四次作业
查看>>
django迁移数据库错误
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
字符串处理
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
距离公式汇总以及Python实现
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>
PyQt5--EventSender
查看>>
Sql Server 中由数字转换为指定长度的字符串
查看>>
tmux的简单快捷键
查看>>