java调用webservice接口,并解析返回参数问题
作者:weixin_47056195
这篇文章主要介绍了java调用webservice接口,并解析返回参数问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
java调用webservice接口,并解析返回参数
1. 设置传参
例如以下格式:
// 确定传参格式以及赋值 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. 调用对端接口
//方法调用 //reqXml 传入的参数信息 //applyInfoUrl 对端接口的请求地址 //type 对端接口的方法名,如果只是调用一个方法名可以写死 String result = DocumentTrans.send(reqXml,applyInfoUrl,type);
调用实现代码
//调用接口 public static String send(String params,String url,String type) { log.info("===传递的参数==="+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、设置参数 in0(对端的方法中的参数名) call.addParameter("in0", org.apache.axis.encoding.XMLType.XSD_STRING, //参数类型 javax.xml.rpc.ParameterMode.IN);// 接口的参数 // 设置返回类型 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.解析返回的参数
转为List<Map<String, String>>
//2.1 先调用解析 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 转换 将解析出来的数据document 传入转换接口Documentanalysis1 public static List<Map<String, String>> Documentanalysis1(Document doc) { List<Map<String, String>> uploadList = new ArrayList<Map<String, String>>(); Element html = doc.getRootElement();// 获取根结点 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);// 获取子结点 elements.forEach(b -> { Map<String, String> uploadMap = new HashMap<>(); uploadMap.put(b.getName(), b.getText()); uploadList.add(uploadMap); }); }); //返回List<Map<String, String>> return uploadList; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。