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)用一個(gè)方法名可以寫死 String result = DocumentTrans.send(reqXml,applyInfoUrl,type);
調(diào)用實(shí)現(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é)點(diǎn)
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é)點(diǎn)
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
CentOS?7.9服務(wù)器Java部署環(huán)境配置的過程詳解
這篇文章主要介紹了CentOS?7.9服務(wù)器Java部署環(huán)境配置,主要包括ftp服務(wù)器搭建過程、jdk安裝方法以及mysql安裝過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
java數(shù)據(jù)結(jié)構(gòu)與算法數(shù)組模擬隊(duì)列示例詳解
這篇文章主要為大家介紹了java數(shù)據(jù)結(jié)構(gòu)與算法數(shù)組模擬隊(duì)列示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解
這篇文章主要介紹了java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解,jar包實(shí)現(xiàn)了特定功能的,java字節(jié)碼文件的壓縮包,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-07-07
Spring Boot 啟動(dòng)失敗:循環(huán)依賴排查到懶加載配置的過程解析
本文我將從一個(gè)真實(shí)的生產(chǎn)環(huán)境故障案例出發(fā),帶你深入了解Spring Boot循環(huán)依賴的檢測機(jī)制、排查方法和解決方案,通過系統(tǒng)性分析和實(shí)戰(zhàn)演練幫助掌握如何在復(fù)雜的應(yīng)用中處理循環(huán)依賴,感興趣的朋友跟隨小編一起看看吧2025-08-08
微服務(wù)搭建集成Spring Cloud Turbine詳解
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發(fā)便利性巧妙地簡化了分布式系統(tǒng)基礎(chǔ)設(shè)施的開發(fā),最終給開發(fā)者留出了一套簡單易懂、易部署和易維護(hù)的分布式系統(tǒng)開發(fā)工具包。下面我們來詳細(xì)了解一下吧2019-06-06
Caffeine本地緩存核心原理與使用方法詳解(含與Spring?Cache集成)
本文介紹Caffeine作為高性能本地緩存庫,具備智能淘汰策略和并發(fā)優(yōu)化,集成SpringCache實(shí)現(xiàn)緩存管理,探討緩存穿透、擊穿、雪崩防護(hù)及多級緩存(Caffeine+Redis)方案,適用于Java生態(tài)的緩存實(shí)踐,感興趣的朋友跟隨小編一起看看吧2025-09-09

