传参和返参的效果图如下:
传参:
返参:
代码实现:
1、最外层类
java">/*** 外层DATA类*/
@XmlRootElement(name = "DATA")
public class PointsXmlData {private int rltFlag;private int failType;private String failMemo;private PointsDetailLists detailLists;//数据集合@XmlElement(name = "RLT_FLAG")public int getRltFlag() { return rltFlag; }public void setRltFlag(int rltFlag) { this.rltFlag = rltFlag; }@XmlElement(name = "FAIL_TYPE")public int getFailType() { return failType; }public void setFailType(int failType) { this.failType = failType; }@XmlElement(name = "FAIL_MEMO")public String getFailMemo() { return failMemo; }public void setFailMemo(String failMemo) { this.failMemo = failMemo; }@XmlElement(name = "DETAIL_LISTS")public PointsDetailLists getDetailLists() { return detailLists; }public void setDetailLists(PointsDetailLists detailLists) { this.detailLists = detailLists; }
}
2、中间层类
java">/*** 中间层DETAIL_LISTS类*/
@XmlAccessorType(XmlAccessType.FIELD)
public class PointsDetailLists {@XmlElement(name = "DETAIL_LIST")private List<PointsList> detailList;//每一个数据类public List<PointsList> getDetailList() { return detailList; }public void setDetailList(List<PointsList> detailList) { this.detailList = detailList; }
}
3、详细数据类
java">/*** 详细数据类*/
@XmlAccessorType(XmlAccessType.FIELD)
public class PointsList {/*** 用户ID*/@XmlElement(name = "USER_ID")private String userId;/*** 数量*/@XmlElement(name = "NUM")private int num;public String getUserId() { return userId;}public void setUserId(String userId) {this.userId = userId;}public int getNum() { return num;}public void setNum(int num) {this.num = num;}
}
4、通过传参XML的方式调用第三方系统获取数据
java">private CornerNumVO queryPointsBadge(String userId) {CornerNumVO numVO = new CornerNumVO();try {URL url = new URL("http://127.0.0.1........");//TODO: 替换为要调用的接口地址HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "application/xml; utf-8");connection.setDoOutput(true);//组装xmlString xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +"<DATA>" +"<DETAIL_LISTS>" +"<DETAIL_LIST>" +"<USER_ID>"+ userId +"</USER_ID>" +"</DETAIL_LIST>" +"</DETAIL_LISTS>" +"</DATA>";//写入请求中byte[] input = xmlData.getBytes("utf-8");OutputStream os = connection.getOutputStream();os.write(input, 0, input.length);os.flush();// 获取响应int responseCode = connection.getResponseCode();// 成功响应,得到数据if (responseCode == HttpURLConnection.HTTP_OK) {BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String inputLine;StringBuilder dataStr = new StringBuilder();//判断每一行while ((inputLine = in.readLine()) != null) {dataStr.append(inputLine);}in.close();//解析返回的字符串并组装数据JAXBContext context = JAXBContext.newInstance(PointsXmlData.class);Unmarshaller unmarshaller = context.createUnmarshaller();//转换数据StringReader reader = new StringReader(dataStr.toString());PointsXmlData data = (PointsXmlData) unmarshaller.unmarshal(reader);//组装返回考核方案下的数据为实体类for (PointsList detail : data.getDetailLists().getDetailList()) {//TODO: 自己的逻辑处理......System.out.println("USER_ID: " + detail.getUserId() + ", NUM: " + detail.getNum());}} else {//TODO: 自己的逻辑处理......return new CornerNumVO();}} catch (Exception e) {e.printStackTrace();}return numVO;}