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();
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM