码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-07-28 15:11:12      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:send   factor   pos   org   log   tac   参数   字符串   信息   

 

参考文章: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试试调用接口的方法

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

标签:send   factor   pos   org   log   tac   参数   字符串   信息   

原文地址:https://www.cnblogs.com/bzsz-quan/p/9381766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!