4g挂牌对接,可以接收到 心跳日志, 上传的音频文件 ,保存到数据库和本地文件
This commit is contained in:
34
src/main/java/com/rj/common/IntegerDeserializer.java
Normal file
34
src/main/java/com/rj/common/IntegerDeserializer.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.rj.common;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 自定义 Integer 反序列化器
|
||||
* 支持从字符串和整数两种格式反序列化为 Integer
|
||||
* 如果无法解析,返回 null
|
||||
*/
|
||||
public class IntegerDeserializer extends JsonDeserializer<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
String value = p.getValueAsString();
|
||||
if (value == null || value.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 尝试直接解析为整数
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
// 如果无法解析为整数,返回 null
|
||||
// 这样可以避免反序列化错误,字段会被设置为 null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
65
src/main/java/com/rj/common/LocalDateTimeDeserializer.java
Normal file
65
src/main/java/com/rj/common/LocalDateTimeDeserializer.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package com.rj.common;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 自定义 LocalDateTime 反序列化器
|
||||
* 支持多种时间格式,统一转换为应用配置时区的 LocalDateTime
|
||||
* 支持格式:
|
||||
* - 2025-11-29T18:13:02.1670603
|
||||
* - 2025-11-29T18:13:02.1670603+08:00
|
||||
* - 2025-11-29T18:13:02Z
|
||||
* - 2025-11-29 18:13:02
|
||||
*/
|
||||
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
|
||||
|
||||
@Override
|
||||
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
String timeStr = p.getValueAsString();
|
||||
if (timeStr == null || timeStr.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 检测是否包含时区信息:以Z结尾,或包含时区偏移格式(+HH:MM 或 -HH:MM)
|
||||
boolean hasTimezone = timeStr.endsWith("Z") ||
|
||||
timeStr.matches(".*[+-]\\d{2}:\\d{2}(?:\\d{2})?$");
|
||||
|
||||
if (hasTimezone) {
|
||||
// 处理ISO 8601格式,包含时区信息
|
||||
ZonedDateTime zonedDateTime;
|
||||
if (timeStr.endsWith("Z")) {
|
||||
// UTC时间,转换为应用配置的时区
|
||||
zonedDateTime = ZonedDateTime.parse(timeStr.replace("Z", "+00:00")).withZoneSameInstant(TimeZoneUtils.getZoneId());
|
||||
} else {
|
||||
// 带时区偏移的时间,转换为应用配置的时区
|
||||
zonedDateTime = ZonedDateTime.parse(timeStr).withZoneSameInstant(TimeZoneUtils.getZoneId());
|
||||
}
|
||||
return zonedDateTime.toLocalDateTime();
|
||||
} else {
|
||||
// 没有时区信息,假设是应用配置时区的时间
|
||||
String cleanTimeStr = timeStr;
|
||||
if (cleanTimeStr.contains("T")) {
|
||||
cleanTimeStr = cleanTimeStr.replace("T", " ");
|
||||
}
|
||||
if (cleanTimeStr.contains(".")) {
|
||||
cleanTimeStr = cleanTimeStr.substring(0, cleanTimeStr.indexOf("."));
|
||||
}
|
||||
if (cleanTimeStr.length() > 19) {
|
||||
cleanTimeStr = cleanTimeStr.substring(0, 19);
|
||||
}
|
||||
return LocalDateTime.parse(cleanTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IOException("解析时间字符串失败: " + timeStr, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
60
src/main/java/com/rj/common/TimeZoneUtils.java
Normal file
60
src/main/java/com/rj/common/TimeZoneUtils.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.rj.common;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.rj.config.AppConfig;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
* 时区工具类
|
||||
* 提供统一的时区相关工具方法
|
||||
*
|
||||
* @author Auto Generated
|
||||
* @since 2025-01-01
|
||||
*/
|
||||
@Component
|
||||
public class TimeZoneUtils {
|
||||
|
||||
private static AppConfig appConfig;
|
||||
|
||||
@Autowired
|
||||
public void setAppConfig(AppConfig appConfig) {
|
||||
TimeZoneUtils.appConfig = appConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用配置的时区ID
|
||||
*
|
||||
* @return ZoneId
|
||||
*/
|
||||
public static ZoneId getZoneId() {
|
||||
String timezone = appConfig != null ? appConfig.getTimezone() : "Asia/Shanghai";
|
||||
return ZoneId.of(timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时区的当前时间
|
||||
*
|
||||
* @return LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime now() {
|
||||
return ZonedDateTime.now(getZoneId()).toLocalDateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定时区的时间转换为应用时区的LocalDateTime
|
||||
*
|
||||
* @param zonedDateTime 带时区的时间
|
||||
* @return LocalDateTime
|
||||
*/
|
||||
public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime) {
|
||||
if (zonedDateTime == null) {
|
||||
return null;
|
||||
}
|
||||
return zonedDateTime.withZoneSameInstant(getZoneId()).toLocalDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user