1.首先在idea安装axis2。
链接:https://share.weiyun.com/5vam26a 密码:rqnsby
2.axis2工具类。
package com.lxp.sdk.Util;
import com.alibaba.fastjson.JSON;
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 javax.xml.namespace.QName;
import java.util.Iterator;
import java.util.Map;
/**
* axis2工具类
*
* @author lxp
* @date 2019 -07-19 09:27:17
*/
public class Axis2Util {
public static String xcloudWebService(Map map) {
String returnResult = null;
try {
//服务端地址
String soapBindingAddress = "http://p2p1.lxp.com:8008/conf/xcloudwebs.wsdl";
//参数转json字符串
String params = JSON.toJSONString(map);
ServiceClient sender = new ServiceClient();
EndpointReference endpointReference = new EndpointReference(soapBindingAddress);
Options options = new Options();
options.setTo(endpointReference);
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
//设置命名空间和方法名
OMNamespace omNs = fac.createOMNamespace("urn:xcloudwebs", "");
OMElement data = fac.createOMElement("xcloudwebs-api", omNs);
// 对应参数的节点
String[] strs = new String[]{"params"};
// 参数值 ,以json的格式进行传递
String[] val = new String[]{params};
QName qname = new QName(strs[0]);
OMElement inner = fac.createOMElement(qname);
inner.setText(val[0]);
data.addChild(inner);
// 发送数据,返回结果
OMElement result = sender.sendReceive(data);
//返回的数据解析,返回的是json格式的数据,对string进行jsonobject
Iterator iterator = result.getChildElements();
OMElement OMEResult = null;
while (iterator.hasNext()) {
OMEResult = (OMElement) iterator.next();
}
//获取结果解析
returnResult = OMEResult.getText();
} catch (AxisFault ex) {
ex.printStackTrace();
}
return returnResult;
}
}
3.测试类:
package com.lxp.sdk;
import com.lxp.sdk.Util.Axis2Util;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2019/7/18.
*/
public class WebServiceClient {
public static void main(String[] args) {
try {
Map map = new HashMap();
map.put("method",1004);
map.put("key","4c334028680d196c1f09ffc22909e67d");
map.put("secret","a82c0ba27fa951f54a39505ee743b2fd");
map.put("gateway_uid","867725038846618");
map.put("old_password","4477b36f96e3b57f6d706030f5839d89");
map.put("new_password","e10adc3949ba59abbe56e057f20f883e");
String result = Axis2Util.xcloudWebService(map);
System.out.println("========== 结果 ==========");
System.out.println(result);
}catch (Exception ex){
ex.printStackTrace();
}
}
}
解析:
1.支持多个参数传入。
2.将参数以map的形式传入,转换为json字符串。
3.成功之后返回服务端固定格式,有需要再额外处理。