如何調用別人提供的webservice接口

當我們拿到一個接口的時候,先別急着去調用它,我們得先測試這個接口是否正確,是否能調用成功,以及返回的數據是否是我們需要的類型等等。這時候我們需要一個工具,比如SoapUI。(最好用綠色免安裝版的。)然後去測試接口的可行性。

 

可行之後再帶入咱們的代碼裏面。這裏需要用到CXF插件,百度隨處可下。下面是我調用webservice的代碼,參數分別爲接口地址,調用接口的方法名以及方法的參數。非常的簡單。

 

public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
if (!url.endsWith("wsdl")) {
url += "?wsdl";
}
org.apache.cxf.endpoint.Client client = dcf.createClient(url);
//處理webService接口和實現類namespace不同的情況,CXF動態客戶端在處理此問題時,會報No operation was found with the name的異常
Endpoint endpoint = client.getEndpoint();
QName opName = new QName(endpoint.getService().getName().getNamespaceURI(),operation);
BindingInfo bindingInfo= endpoint.getEndpointInfo().getBinding();
if(bindingInfo.getOperation(opName) == null){
for(BindingOperationInfo operationInfo : bindingInfo.getOperations()){
if(operation.equals(operationInfo.getName().getLocalPart())){
opName = operationInfo.getName();
break;
}
}
}
Object[] res = null;
try {
res = client.invoke(opName, parameters);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}

 

ps:

對於一個懶人來說,很多東西不一定要懂,只需要會用則好。好比窮人手裏的100塊和富人手裏的100塊完全就是兩個概念,懶人的100分精力就相當於窮人手裏的100塊,當然不能亂花。有人會說,你上網玩遊戲逛街的時候精力怕是有1w!!我只能回答,精力需要對事對人。或許我們這種人的成就很低,甚至沒有成就,一生庸庸碌碌~~~ 可能我的價值觀比較低吧,覺得沒所謂,日子能過就好,就好像牛排和豬肉,它們帶給我的味覺衝擊其實是差不多的,但是豬肉更便宜,我當然會選擇豬肉!!

不愛展望未來,但喜珍惜當下~

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章