From 29f1ae38d509ff869544aeef752914d66cdde976 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Tue, 22 Aug 2023 12:05:20 +0800 Subject: [PATCH] =?UTF-8?q?xml=20=E7=BC=96=E7=A0=81=E6=B5=8B=E8=AF=95=20?= =?UTF-8?q?=E5=8F=8A=20=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docking/gb28181/common/xml/XmlUtils.java | 6 +++- gb28181-service/pom.xml | 5 ++++ .../sip/gb28181/constant/GB28181Constant.java | 1 + .../request/MessageRequestProcessor.java | 4 +-- .../dto/{MessageDto.java => MessageDTO.java} | 2 +- .../record/dto/RecordInfoRequestDTO.java | 28 +++++++++++++++++++ .../gb28181/core/sip/message/dto/DTOTest.java | 21 ++++++++++++++ 7 files changed, 63 insertions(+), 4 deletions(-) rename gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/dto/{MessageDto.java => MessageDTO.java} (95%) create mode 100644 gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/record/dto/RecordInfoRequestDTO.java create mode 100644 gb28181-service/src/test/java/cn/skcks/docking/gb28181/core/sip/message/dto/DTOTest.java diff --git a/common/src/main/java/cn/skcks/docking/gb28181/common/xml/XmlUtils.java b/common/src/main/java/cn/skcks/docking/gb28181/common/xml/XmlUtils.java index addd0b3..be64677 100644 --- a/common/src/main/java/cn/skcks/docking/gb28181/common/xml/XmlUtils.java +++ b/common/src/main/java/cn/skcks/docking/gb28181/common/xml/XmlUtils.java @@ -1,5 +1,6 @@ package cn.skcks.docking.gb28181.common.xml; +import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -7,12 +8,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; import java.nio.charset.Charset; @SuppressWarnings({"unused"}) public class XmlUtils { - private static final ObjectMapper mapper = new XmlMapper(); + private static final XmlMapper mapper = new XmlMapper(); static { mapper.configure(SerializationFeature.INDENT_OUTPUT, true); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); @@ -25,6 +27,8 @@ public class XmlUtils { mapper.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature(), true); // 大驼峰 (首字母大写) mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy()); + // 添加 xml 头部声明 + mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); } public static String toXml(Object obj) { diff --git a/gb28181-service/pom.xml b/gb28181-service/pom.xml index 98a2285..74bbda0 100644 --- a/gb28181-service/pom.xml +++ b/gb28181-service/pom.xml @@ -73,6 +73,11 @@ cn.hutool hutool-all + + org.junit.jupiter + junit-jupiter + test + diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/gb28181/constant/GB28181Constant.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/gb28181/constant/GB28181Constant.java index 6c20d75..3f2e0f8 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/gb28181/constant/GB28181Constant.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/gb28181/constant/GB28181Constant.java @@ -1,6 +1,7 @@ package cn.skcks.docking.gb28181.core.sip.gb28181.constant; public class GB28181Constant { + public static final String TIME_ZONE = "Asia/Shanghai"; public static final String CHARSET = "GB2312"; public static final String GEO_COORD_SYS = "WGS84"; } diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/MessageRequestProcessor.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/MessageRequestProcessor.java index 0f6dcb7..b751330 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/MessageRequestProcessor.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/MessageRequestProcessor.java @@ -3,7 +3,7 @@ package cn.skcks.docking.gb28181.core.sip.message.processor.message.request; import cn.skcks.docking.gb28181.common.json.ResponseStatus; import cn.skcks.docking.gb28181.common.xml.XmlUtils; import cn.skcks.docking.gb28181.core.sip.gb28181.constant.CmdType; -import cn.skcks.docking.gb28181.core.sip.message.processor.message.request.dto.MessageDto; +import cn.skcks.docking.gb28181.core.sip.message.processor.message.request.dto.MessageDTO; import cn.skcks.docking.gb28181.core.sip.gb28181.constant.GB28181Constant; import cn.skcks.docking.gb28181.core.sip.listener.SipListener; import cn.skcks.docking.gb28181.core.sip.message.processor.MessageProcessor; @@ -43,7 +43,7 @@ public class MessageRequestProcessor implements MessageProcessor { String deviceId = SipUtil.getUserIdFromFromHeader(request); CallIdHeader callIdHeader = request.getCallIdHeader(); - MessageDto messageDto = XmlUtils.parse(request.getRawContent(), MessageDto.class, GB28181Constant.CHARSET); + MessageDTO messageDto = XmlUtils.parse(request.getRawContent(), MessageDTO.class, GB28181Constant.CHARSET); log.debug("接收到的消息 => {}", messageDto); DockingDevice device = deviceService.getDevice(deviceId); diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/dto/MessageDto.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/dto/MessageDTO.java similarity index 95% rename from gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/dto/MessageDto.java rename to gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/dto/MessageDTO.java index a69725a..40b7559 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/dto/MessageDto.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/message/request/dto/MessageDTO.java @@ -6,7 +6,7 @@ import lombok.Data; @JacksonXmlRootElement(localName = "xml") @Data -public class MessageDto { +public class MessageDTO { private String cmdType; @JacksonXmlProperty(localName = "SN") diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/record/dto/RecordInfoRequestDTO.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/record/dto/RecordInfoRequestDTO.java new file mode 100644 index 0000000..ba80697 --- /dev/null +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/record/dto/RecordInfoRequestDTO.java @@ -0,0 +1,28 @@ +package cn.skcks.docking.gb28181.core.sip.message.processor.record.dto; + +import cn.hutool.core.date.DatePattern; +import cn.skcks.docking.gb28181.core.sip.gb28181.constant.GB28181Constant; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; +import lombok.Data; + +import java.util.Date; + +@JacksonXmlRootElement(localName = "Query") +@Data +public class RecordInfoRequestDTO { + private String cmdType; + + @JacksonXmlProperty(localName = "SN") + private String sn; + + @JacksonXmlProperty(localName = "DeviceID") + private String deviceId; + + @JsonFormat(pattern = DatePattern.UTC_SIMPLE_PATTERN, timezone = GB28181Constant.TIME_ZONE) + private Date startTime; + + @JsonFormat(pattern = DatePattern.UTC_SIMPLE_PATTERN, timezone = GB28181Constant.TIME_ZONE) + private Date endTime; +} diff --git a/gb28181-service/src/test/java/cn/skcks/docking/gb28181/core/sip/message/dto/DTOTest.java b/gb28181-service/src/test/java/cn/skcks/docking/gb28181/core/sip/message/dto/DTOTest.java new file mode 100644 index 0000000..1c8f26a --- /dev/null +++ b/gb28181-service/src/test/java/cn/skcks/docking/gb28181/core/sip/message/dto/DTOTest.java @@ -0,0 +1,21 @@ +package cn.skcks.docking.gb28181.core.sip.message.dto; + +import cn.hutool.core.date.DateUtil; +import cn.skcks.docking.gb28181.common.xml.XmlUtils; +import cn.skcks.docking.gb28181.core.sip.message.processor.record.dto.RecordInfoRequestDTO; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; + +@Slf4j +public class DTOTest { + @Test + void test(){ + RecordInfoRequestDTO dto = new RecordInfoRequestDTO(); + dto.setCmdType("RecordInfo"); + dto.setDeviceId("44050100001310000006"); + dto.setSn("66666"); + dto.setStartTime(DateUtil.date()); + dto.setEndTime(DateUtil.date()); + log.info("\n{}", XmlUtils.toXml(dto)); + } +}