springboot接口接收xml

对xml文件的操作也可以借助hutoolXmlUtil.

1. xml格式

<root>
	<deviceStatInfo  onlineCount="64" offlineCount="2" errorCount="0"/>
	<data>
		<record id="0" name="5号楼10层流量计" status="预警2/正常1/异常3" time="2024-04-10 12:30"/>
		<record id="1" name="5号楼13层流量计" status="预警2/正常1/异常3" time="2024-04-10 12:30"/>
		<record id="2" name="5号楼16层流量计" status="预警2/正常1/异常3" time="2024-04-10 12:30"/>
		<record id="3" name="5号楼19层流量计" status="预警2/正常1/异常3" time="2024-04-10 12:30"/>
	</data>
</root>

2. 实体类

注意其中XmlElementWrapperXmlElement的位置

点击查看实体类
package com.tky.digital.twin.api.dto;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.List;

/**
 * @author codor
 * @date 2024/04/19 15:31
 */
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "root")
public class EquipmentWaterPipeStatusMessage implements Serializable {

    private static final long serialVersionUID = 1L;

//    @XmlAttribute
    private DeviceStatInfoDto deviceStatInfo;

//    @XmlAttribute
    private List<Record> data;

    @NoArgsConstructor
    @AllArgsConstructor
    public static class DeviceStatInfoDto implements Serializable {

        private int onlineCount;
        private int offlineCount;
        private int errorCount;

        @XmlAttribute
        public int getOnlineCount() {
            return onlineCount;
        }

        public void setOnlineCount(int onlineCount) {
            this.onlineCount = onlineCount;
        }

        @XmlAttribute
        public int getOfflineCount() {
            return offlineCount;
        }

        public void setOfflineCount(int offlineCount) {
            this.offlineCount = offlineCount;
        }

        @XmlAttribute
        public int getErrorCount() {
            return errorCount;
        }

        public void setErrorCount(int errorCount) {
            this.errorCount = errorCount;
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Record implements Serializable {

        private String id;
        private String name;
        private String status;
        private String time;

        @XmlAttribute
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @XmlAttribute
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @XmlAttribute
        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        @XmlAttribute
        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }
    }

    @XmlElement(name = "deviceStatInfo")
    public DeviceStatInfoDto getDeviceStatInfo() {
        return deviceStatInfo;
    }

    public void setDeviceStatInfo(DeviceStatInfoDto deviceStatInfo) {
        this.deviceStatInfo = deviceStatInfo;
    }

    @XmlElementWrapper(name = "data")
    @XmlElement(name = "record")
    public List<Record> getData() {
        return data;
    }

    public void setData(List<Record> data) {
        this.data = data;
    }
}

3. 接口接收

@PostMapping(value = "/waterpipe", produces = {MediaType.APPLICATION_XML_VALUE})
public void waterpipe(@RequestBody EquipmentWaterPipeStatusMessage message) {
    System.out.println("JSONUtil.toJsonStr(message) = " + JSONUtil.toJsonStr(message));
}
posted @ 2024-04-22 09:22  Codorld  阅读(731)  评论(0编辑  收藏  举报