mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-04-04 01:33:23 +08:00
update 优化 LocalDateTime 任意时间格式传参序列化
This commit is contained in:
@@ -76,8 +76,6 @@ spring:
|
||||
format:
|
||||
date-time: yyyy-MM-dd HH:mm:ss
|
||||
jackson:
|
||||
# 日期格式化
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
serialization:
|
||||
# 格式化输出
|
||||
indent_output: false
|
||||
|
||||
@@ -3,11 +3,11 @@ package org.dromara.common.json.config;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.json.handler.BigNumberSerializer;
|
||||
import org.dromara.common.json.handler.CustomDateDeserializer;
|
||||
import org.dromara.common.json.handler.CustomLocalDateTimeDeserializer;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration;
|
||||
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import tools.jackson.databind.ext.javatime.deser.LocalDateTimeDeserializer;
|
||||
import tools.jackson.databind.ext.javatime.ser.LocalDateTimeSerializer;
|
||||
import tools.jackson.databind.module.SimpleModule;
|
||||
import tools.jackson.databind.ser.std.ToStringSerializer;
|
||||
@@ -43,7 +43,7 @@ public class JacksonConfig {
|
||||
module.addSerializer(BigDecimal.class, ToStringSerializer.instance);
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
|
||||
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
|
||||
module.addDeserializer(LocalDateTime.class, new CustomLocalDateTimeDeserializer());
|
||||
module.addDeserializer(Date.class, new CustomDateDeserializer());
|
||||
return module;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class CustomDateDeserializer extends ValueDeserializer<Date> {
|
||||
*/
|
||||
@Override
|
||||
public Date deserialize(JsonParser p, DeserializationContext ctxt) {
|
||||
DateTime parse = DateUtil.parse(p.getText());
|
||||
DateTime parse = DateUtil.parse(p.getString());
|
||||
if (ObjectUtils.isNull(parse)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.dromara.common.json.handler;
|
||||
|
||||
import tools.jackson.core.JsonParser;
|
||||
import tools.jackson.databind.DeserializationContext;
|
||||
import tools.jackson.databind.ValueDeserializer;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 自定义 LocalDateTime 类型反序列化处理器(支持多种格式,无第三方依赖)
|
||||
*
|
||||
* @author AprilWind
|
||||
*/
|
||||
public class CustomLocalDateTimeDeserializer extends ValueDeserializer<LocalDateTime> {
|
||||
|
||||
/** 支持时间的格式列表(直接解析为 LocalDateTime) */
|
||||
private static final List<DateTimeFormatter> DATETIME_FORMATTERS = List.of(
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"),
|
||||
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"),
|
||||
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"),
|
||||
DateTimeFormatter.ofPattern("yyyyMMddHHmmss"),
|
||||
DateTimeFormatter.ofPattern("yyyyMMdd HHmmss"),
|
||||
DateTimeFormatter.ISO_LOCAL_DATE_TIME
|
||||
);
|
||||
|
||||
/** 仅日期的格式列表(解析为 LocalDate,再补零时转 LocalDateTime) */
|
||||
private static final List<DateTimeFormatter> DATE_ONLY_FORMATTERS = List.of(
|
||||
DateTimeFormatter.ISO_LOCAL_DATE,
|
||||
DateTimeFormatter.ofPattern("yyyy/MM/dd"),
|
||||
DateTimeFormatter.BASIC_ISO_DATE
|
||||
);
|
||||
|
||||
/**
|
||||
* 反序列化逻辑:将字符串转换为 LocalDateTime 对象
|
||||
*
|
||||
* @param p JSON 解析器,用于获取字符串值
|
||||
* @param ctxt 上下文环境(可用于获取更多配置)
|
||||
* @return 转换后的 LocalDateTime 对象,若为空字符串返回 null
|
||||
*/
|
||||
@Override
|
||||
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) {
|
||||
String text = p.getString();
|
||||
if (text == null || text.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
text = text.trim();
|
||||
|
||||
// 纯数字:按时间戳处理(毫秒)
|
||||
if (text.chars().allMatch(Character::isDigit)) {
|
||||
return Instant.ofEpochMilli(Long.parseLong(text))
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
}
|
||||
|
||||
// 尝试带时间的格式
|
||||
for (DateTimeFormatter formatter : DATETIME_FORMATTERS) {
|
||||
try {
|
||||
return LocalDateTime.parse(text, formatter);
|
||||
} catch (DateTimeParseException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试仅日期的格式,补零时
|
||||
for (DateTimeFormatter formatter : DATE_ONLY_FORMATTERS) {
|
||||
try {
|
||||
return LocalDate.parse(text, formatter).atStartOfDay();
|
||||
} catch (DateTimeParseException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user