Java中xml转javaBean
maven坐标
dependency>
groupId>com.fasterxml.jackson.dataformatgroupId>
artifactId>jackson-dataformat-xmlartifactId>
version>2.13.4version>
dependency>
代码测试
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import javax.xml.bind.annotation.XmlRootElement;
import java.time.LocalDateTime;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
/**
* Aut服务器托管网hor:daWang
* Date:2023/9/9 16:38
*/
public class XmlTest {
public static void main(String[] args) throws Exception{
String str="rn" +
"192.168.1.228rn" +
"::rn" +
"80rn" +
"HTTPn" +
"08:54:11:a1:98:aarn" +
"1rn" +
"2023-09-08T15:49:45+08:00rn" +
"1rn" +
"videolossrn" +
"inactivern" +
"videoloss alarmrn" +
"测试001rn" +
"";
ObjectMapper objectMapper = new XmlMapper();
EventXml eventXml = objectMapper.readValue(str, EventXml.class);
System.out.println(JSONUtil.toJsonStr(eventXml));
}
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class EventXml{
private String ipAddress;
private String ipv6Address;
private Integer portNo;
private String protocol;
private String macAddress;
private Integer channelID;
private String dateTime;
private Integer activePostCount;
private String eventType;
private String eventState;
private String eventDescription;
private String channelName;
}
}
输出内容
Connected to the target VM, address:服务器托管网 '127.0.0.1:62168', transport: 'socket'
{"ipAddress":"192.168.1.228","ipv6Address":"::","portNo":80,"protocol":"HTTP","macAddress":"08:54:11:a1:98:aa","channelID":1,"dateTime":"2023-09-08T15:49:45+08:00","activePostCount":1,"eventType":"videoloss","eventState":"inactive","eventDescription":"videoloss alarm","channelName":"测试001"}
Disconnected from the target VM, address: '127.0.0.1:62168', transport: 'socket'
Process finished with exit code 0
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
这个逻辑是从一个视频上看来的, b站的某个视频,视频里是小米公司的大拿讲设计方面的方案, 提到了 用refreshTime来作为度量,判断mq积压的程度.refreshTime是一个时间差, 用消费时的当前时间,减去 mq生成时的创建时间, 视频上用的好像叫r…