java調(diào)用webservice接口,并解析返回參數(shù)問題
更新時間:2024年07月02日 11:41:17 作者:weixin_47056195
這篇文章主要介紹了java調(diào)用webservice接口,并解析返回參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
java調(diào)用webservice接口,并解析返回參數(shù)
1. 設(shè)置傳參
例如以下格式:
// 確定傳參格式以及賦值
String reqXml = "<createAppParam>\n" +
"<serviceUserName>auth</serviceUserName>\n" +
"<servicePwd>auth</servicePwd>\n" +
"<rootTicket>"+rootTicket+"</rootTicket>\n" +
"<appAccount>"+userAccount+"</appAccount>\n" +
"<resNum>1000</resNum>\n" +
"<operationCode>"+operationCode+"</operationCode>\n" +
"<functionCode>668801</functionCode>\n" +
"<authMode>"+mode+"</authMode>\n" +
"<applyReason>"+sendApplyParam.getCerReason()+"</applyReason>\n" +
"<userTimes>9999</userTimes>\n" +
"<duration>"+sendApplyParam.getExpire()+"</duration>\n" +
"<userIP>"+ip+"</userIP>\n" +
"<selectedApprover>"+sendApplyParam.getApproverId()+"</selectedApprover>\n" +
"<workOrderID>999999</workOrderID>\n" +
"<workOrderType>9999</workOrderType>\n" +
"</createAppParam>\n";2. 調(diào)用對端接口
//方法調(diào)用 //reqXml 傳入的參數(shù)信息 //applyInfoUrl 對端接口的請求地址 //type 對端接口的方法名,如果只是調(diào)用一個方法名可以寫死 String result = DocumentTrans.send(reqXml,applyInfoUrl,type);
調(diào)用實現(xiàn)代碼
//調(diào)用接口
public static String send(String params,String url,String type) {
log.info("===傳遞的參數(shù)==="+params);
log.info("===請求路徑==="+url);
String result="";
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = null;
try {
call = service.createCall();
} catch (ServiceException e) {
e.printStackTrace();
}
//10.174.242.24:7001
call.setTargetEndpointAddress(url);
//3、設(shè)置參數(shù) in0(對端的方法中的參數(shù)名)
call.addParameter("in0",
org.apache.axis.encoding.XMLType.XSD_STRING, //參數(shù)類型
javax.xml.rpc.ParameterMode.IN);// 接口的參數(shù)
// 設(shè)置返回類型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
try {
result = (String) call.invoke(QName.valueOf(type), new Object[]{params});
} catch (Exception e) {
e.printStackTrace();
throw new NrmsYnException("接口異常");
}
return result;
}2.解析返回的參數(shù)
轉(zhuǎn)為List<Map<String, String>>
//2.1 先調(diào)用解析
public static Document DocumentHelperreadStringXml(String xmlContent) {
// DocumentHelper 解析xml字符串
Document document = null;
try {
document = DocumentHelper.parseText(xmlContent);
} catch (DocumentException e1) {
e1.printStackTrace();
}
return document;
}
//2.2 轉(zhuǎn)換 將解析出來的數(shù)據(jù)document 傳入轉(zhuǎn)換接口Documentanalysis1
public static List<Map<String, String>> Documentanalysis1(Document doc) {
List<Map<String, String>> uploadList = new ArrayList<Map<String, String>>();
Element html = doc.getRootElement();// 獲取根結(jié)點
List<Element> head = html.elements();
Set<String> set = new HashSet<>();
head.forEach(a -> {
set.add(a.getName());
});
set.forEach(a -> {
List<Element> elements = html.elements(a);// 獲取子結(jié)點
elements.forEach(b -> {
Map<String, String> uploadMap = new HashMap<>();
uploadMap.put(b.getName(), b.getText());
uploadList.add(uploadMap);
});
});
//返回List<Map<String, String>>
return uploadList;
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Myeclipse鏈接Oracle等數(shù)據(jù)庫時lo exception: The Network Adapter coul
今天小編就為大家分享一篇關(guān)于Myeclipse鏈接Oracle等數(shù)據(jù)庫時lo exception: The Network Adapter could not establish the connection,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java?通過手寫分布式雪花SnowFlake生成ID方法詳解
SnowFlake是twitter公司內(nèi)部分布式項目采用的ID生成算法,開源后廣受國內(nèi)大廠的好評。由這種算法生成的ID,我們就叫做SnowFlakeID,下面我們來詳細看看2022-04-04
通過Maven進行jedis連接redis的實現(xiàn)
這篇文章主要介紹了通過Maven進行jedis連接redis的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

