fix:【框架】TimestampLocalDateTimeSerializer 的 fieldName 为空的情况,对应 https://github.com/YunaiV/ruoyi-vue-pro/issues/1032

This commit is contained in:
YunaiV 2025-12-06 09:28:00 +08:00
parent cdc4255da1
commit 0df951079b

View File

@ -23,10 +23,11 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi
@Override @Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 情况一 JsonFormat 自定义注解则使用它https://github.com/YunaiV/ruoyi-vue-pro/pull/1019
String fieldName = gen.getOutputContext().getCurrentName(); String fieldName = gen.getOutputContext().getCurrentName();
if (fieldName != null) {
Class<?> clazz = gen.getOutputContext().getCurrentValue().getClass(); Class<?> clazz = gen.getOutputContext().getCurrentValue().getClass();
Field field = ReflectUtil.getField(clazz, fieldName); Field field = ReflectUtil.getField(clazz, fieldName);
// 情况一 JsonFormat 自定义注解则使用它https://github.com/YunaiV/ruoyi-vue-pro/pull/1019
JsonFormat[] jsonFormats = field.getAnnotationsByType(JsonFormat.class); JsonFormat[] jsonFormats = field.getAnnotationsByType(JsonFormat.class);
if (jsonFormats.length > 0) { if (jsonFormats.length > 0) {
String pattern = jsonFormats[0].pattern(); String pattern = jsonFormats[0].pattern();
@ -34,6 +35,7 @@ public class TimestampLocalDateTimeSerializer extends JsonSerializer<LocalDateTi
gen.writeString(formatter.format(value)); gen.writeString(formatter.format(value));
return; return;
} }
}
// 情况二默认将 LocalDateTime 对象转换为 Long 时间戳 // 情况二默认将 LocalDateTime 对象转换为 Long 时间戳
gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());