Java 如何传参xml调用接口获取数据

传参和返参的效果图如下:

传参:
返参:

代码实现:

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);
            //组装xml
            String 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;
    }
相关推荐
tan180°几秒前
Cpp::C++11右值引用与移动构造(30)
开发语言·数据结构·c++·后端·算法
编程小筑3 分钟前
Clojure语言的面向对象编程
开发语言·后端·golang
Allen Bright13 分钟前
【JVM-1】深入解析JVM:Java虚拟机的核心原理与工作机制
java·开发语言·jvm
hnmpf14 分钟前
flask-admin 在modelview 默认视图下重写create_model_actions来实现列表数据的批量处理actions
后端·python·flask
中國移动丶移不动32 分钟前
深入解读五种常见 Java 设计模式及其在 Spring 框架中的应用
java·后端·spring·设计模式·mybatis
兜里ヌ有糖32 分钟前
Spring——自动装配
java·后端·spring
m0_6724496033 分钟前
springmvc前端传参,后端接收
java·前端·spring
C++小厨神37 分钟前
SQL语言的函数实现
开发语言·后端·golang
等一场春雨1 小时前
Java 分布式锁:Redisson、Zookeeper、Spring 提供的 Redis 分布式锁封装详解
java·分布式·java-zookeeper
上海拔俗网络1 小时前
“AI智能实训系统:让学习更高效、更轻松!
java·团队开发