axis2 对webservice进行接口的调用

axis2 就不说太多了,我也不太懂,在apache上自己下载axis的jar包,导入到项目中,

下面就是实现代码了:

import java.util.Iterator;

import javax.xml.namespace.QName;

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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Demo {


private static void axis2WebService() {
try {
String soapBindingAddress = "http://192.168.0.55:9080/itsm/schemas/ProductServices?wsdl";
ServiceClient sender = new ServiceClient();
EndpointReference endpointReference = new EndpointReference(soapBindingAddress);
Options options = new Options();

options.setTo(endpointReference);
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
// 这个和qname差不多,设置命名空间
OMNamespace omNs = fac.createOMNamespace("http://www.chinawiserv.com/onecenter",""); //这个是namespace的str
OMElement data = fac.createOMElement("getProducts", omNs);   //getProducts是方法
// 对应参数的节点
String[] strs = new String[] { "arg0" };
// 参数值 ,以json的格式进行传递
String[] val = new String[] { "{"userId":"1"}"};
for (int i = 0; i < strs.length; i++) {
QName qname=new QName(strs[i]);
OMElement inner = fac.createOMElement(qname);
inner.setText(val[i]);
data.addChild(inner);
}
System.out.println(data);
// 发送数据,返回结果
OMElement result = sender.sendReceive(data);
System.out.println(result.toString());
//下面是返回的数据解析,返回的是json格式的数据,对string进行jsonobject
Iterator iterator = result.getChildElements();

OMElement result1 = null;
while (iterator.hasNext()) {
result1 = (OMElement) iterator.next();
System.out.println(result1.getText());
}

String re = result1.getText();
JSONObject json_test = JSON.parseObject(re);

System.out.println(json_test.getString("info"));
System.out.println(json_test.getString("result"));
} catch (AxisFault ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
axis2WebService();
}
}

作者:战旗 内容声明: 本内容属自己学习使用 ,若有抄袭情邮件(zhanqi3712@qq.com)告知 ,本人会尽快删除
原文地址:https://www.cnblogs.com/liuyun-10/p/7600071.html