feat(jackson): 添加 LocalDateTime 和 LocalDate 的 JSON 序列化支持
- 引入 LocalDateTimeSerializer 和 LocalDateTimeDeserializer 处理 LocalDateTime 类型 - 添加 LocalDateSerializer 和 LocalDateDeserializer 处理 LocalDate 类型 - 配置 Jackson 使用自定义的时间格式化器 - 扩展 ObjectMapper 配置以支持新的序列化器和反序列化器 - 实现时间类型的空值安全处理 - 统一时间格式为 "yyyy-MM-dd HH:mm:ss" 和 "yyyy-MM-dd"
This commit is contained in:
@@ -11,7 +11,10 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
@@ -28,11 +31,85 @@ public class JacksonConfig {
|
|||||||
jacksonObjectMapperBuilder.locale(Locale.CHINA);
|
jacksonObjectMapperBuilder.locale(Locale.CHINA);
|
||||||
jacksonObjectMapperBuilder.timeZone(TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai")));
|
jacksonObjectMapperBuilder.timeZone(TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai")));
|
||||||
jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
jacksonObjectMapperBuilder.serializers(new LongSerializer());
|
jacksonObjectMapperBuilder.serializers(new LongSerializer(), new LocalDateTimeSerializer(), new LocalDateSerializer());
|
||||||
jacksonObjectMapperBuilder.deserializers(new LongDeserializer());
|
jacksonObjectMapperBuilder.deserializers(new LongDeserializer(), new LocalDateTimeDeserializer(), new LocalDateDeserializer());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
|
||||||
|
|
||||||
|
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
gen.writeNull();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
gen.writeString(value.format(formatter));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<LocalDateTime> handledType() {
|
||||||
|
return LocalDateTime.class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
|
||||||
|
|
||||||
|
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
if (p.getText() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return LocalDateTime.parse(p.getText(), formatter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> handledType() {
|
||||||
|
return LocalDateTime.class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LocalDateSerializer extends JsonSerializer<LocalDate> {
|
||||||
|
|
||||||
|
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
gen.writeNull();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
gen.writeString(value.format(formatter));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<LocalDate> handledType() {
|
||||||
|
return LocalDate.class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
|
||||||
|
|
||||||
|
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
if (p.getText() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return LocalDate.parse(p.getText(), formatter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> handledType() {
|
||||||
|
return LocalDate.class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class LongSerializer extends JsonSerializer<Long> {
|
public static class LongSerializer extends JsonSerializer<Long> {
|
||||||
@Override
|
@Override
|
||||||
public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||||
|
|||||||
Reference in New Issue
Block a user