参考文章:/wangyu/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.mons.schema
XmlSchema
${XmlSchema.version}
wsdl4j
wsdl4j
${wsdl4j.version}
commons-logging
commons-logging
${commons-logging.version}
org.mons.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", "06150939108");
obj.addProperty("transaction_id", "");
obj.addProperty("HisRefundNo", "061510081109");
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试试调用接口的方法