xml 编码测试 及 调整

This commit is contained in:
shikong 2023-08-22 12:05:20 +08:00
parent 62a1fa4521
commit 29f1ae38d5
7 changed files with 63 additions and 4 deletions

View File

@ -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) {

View File

@ -73,6 +73,11 @@
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -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";
}

View File

@ -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);

View File

@ -6,7 +6,7 @@ import lombok.Data;
@JacksonXmlRootElement(localName = "xml")
@Data
public class MessageDto {
public class MessageDTO {
private String cmdType;
@JacksonXmlProperty(localName = "SN")

View File

@ -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;
}

View File

@ -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));
}
}