浏览代码

localdateTime序列化更新

chenjun 1 年之前
父节点
当前提交
e79f72989e

+ 44 - 8
zjugis-framework/zjugis-spring-boot-starter-web/src/main/java/com/zjugis/framework/jackson/core/databind/LocalDateTimeSerializer.java

@@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.ser.ContextualSerializer;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
@@ -21,25 +22,60 @@ import java.time.format.DateTimeFormatter;
  */
 public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> implements ContextualSerializer {
 
-    private JsonFormat jsonFormat;
-
     public static final LocalDateTimeSerializer INSTANCE = new LocalDateTimeSerializer();
+    private JsonFormat jsonFormat;
 
     @Override
     public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
-        if(jsonFormat != null){
-            String pattern = jsonFormat.pattern();
-            if(StringUtils.isNotBlank(pattern)){
-                gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));
-                return;
+        // 获取value来源的类
+        Class<?> aClass = gen.getCurrentValue().getClass();
+
+        // 获取字段名
+        String currentName = gen.getOutputContext().getCurrentName();
+
+        // 获取字段
+        Field declaredField = null;
+        try {
+            declaredField = getDeclaredField(aClass, currentName);
+        } catch (NoSuchFieldException e) {
+            e.printStackTrace();
+        }
+        if (declaredField != null) {
+            // 是否被@JsonFormat修饰
+            boolean annotationPresent = declaredField.isAnnotationPresent(JsonFormat.class);
+
+            if (annotationPresent) {
+                String pattern = declaredField.getAnnotation(JsonFormat.class).pattern();
+                if (StringUtils.isNotEmpty(pattern)) {
+                    gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));
+                } else {
+                    gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
+
+                }
+            } else {
+                gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
             }
+        } else {
+            gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
         }
-        gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
+
     }
+
+    private Field getDeclaredField(Class<?> aClass, String currentName) throws NoSuchFieldException {
+        Field declaredField;
+        try {
+            declaredField = aClass.getDeclaredField(currentName);
+        } catch (NoSuchFieldException e) {
+            declaredField = getDeclaredField(aClass.getSuperclass(), currentName);
+        }
+        return declaredField;
+    }
+
     @Override
     public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
         JsonFormat annotation = property.getAnnotation(JsonFormat.class);
         this.jsonFormat = annotation;
         return this;
     }
+
 }