【问题标题】:How to serialize an Instant without nanoseconds using Spring Boot with Jackson?如何使用 Spring Boot 和 Jackson 序列化 Instant 而无需纳秒?
【发布时间】:2021-07-03 04:34:33
【问题描述】:

Spring 使用 Jackson 的 InstantSerializer 来写出我的 Instant 字段,如下所示:

now: "2021-04-07T10:51:53.043320Z"

不过,我不想要纳秒 - 只是毫秒。我猜是设置应用程序属性

spring.jackson.serialization.write-date-timestamps-as-nanoseconds=false

会实现这一点,但没有区别。

如何告诉 Spring/Jackson 在序列化 Instant 时省略纳秒?

(我使用的是 Spring Boot 2.2.11.RELEASE)

更新

我最终得到了它的工作,基于this answer。我不得不使用已弃用的 JSR310Module 而不是 JavaTimeModule,并重写 createContextual(...) 方法以强制它始终使用我的序列化程序。

@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    ObjectMapper objectMapper = builder.createXmlMapper(false).build();
    JSR310Module jsr310Module = new JSR310Module();
    jsr310Module.addSerializer(Instant.class, new MyInstantSerializer());
    objectMapper.registerModule(jsr310Module);
    return objectMapper;
}

private static class MyInstantSerializer extends InstantSerializer {
    public MyInstantSerializer() {
        super(InstantSerializer.INSTANCE, false, false, 
                new DateTimeFormatterBuilder().appendInstant(3).toFormatter());
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) {
        return this;
    }
}

这也有效(基于Volodya's answer below):

@Bean
public Jackson2ObjectMapperBuilderCustomizer addCustomTimeSerialization() {
    return jacksonObjectMapperBuilder -> 
            jacksonObjectMapperBuilder.serializerByType(Instant.class, new JsonSerializer<Instant>() {

        private final DateTimeFormatter formatter = 
                new DateTimeFormatterBuilder().appendInstant(3).toFormatter();

        @Override
        public void serialize(
                Instant instant, JsonGenerator generator, SerializerProvider provider) throws IOException {
            generator.writeString(formatter.format(instant));
        }
    });
}

【问题讨论】:

    标签: java spring-boot jackson jackson-databind http-message-converter


    【解决方案1】:

    为此,您可以使用@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss[.SSS]")

    完整示例:

        import com.fasterxml.jackson.annotation.JsonFormat;
        import java.time.LocalDateTime;
        import java.time.ZonedDateTime;
        
        public class Message {
        
            @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss[.SSS]")
            private final LocalDateTime dateTime;
        
            @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss[.SSS]")
            private final ZonedDateTime zonedDateTime;
        
            public Message(ZonedDateTime zonedDateTime) {
                this(zonedDateTime.toLocalDateTime(), zonedDateTime);
            }
        
            public Message(LocalDateTime dateTime, ZonedDateTime zonedDateTime) {
                this.dateTime = dateTime;
                this.zonedDateTime = zonedDateTime;
            }
        
            public LocalDateTime getDateTime() {
                return dateTime;
            }
        
            public ZonedDateTime getZonedDateTime() {
                return zonedDateTime;
            }
        }
    
    

    测试:

        import com.fasterxml.jackson.core.JsonProcessingException;
        import com.fasterxml.jackson.databind.ObjectMapper;
        import org.junit.jupiter.api.Test;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.boot.test.autoconfigure.json.JsonTest;
        import java.time.*;
        import java.time.temporal.ChronoUnit;
        import static org.junit.jupiter.api.Assertions.*;
        
        @JsonTest
        class MessageTest {
        
            @Autowired
            ObjectMapper mapper;
        
            @Test
            public void serializationTest() throws JsonProcessingException {
                final LocalDate date = LocalDate.of(2000, Month.JANUARY, 1);
                final LocalTime time = LocalTime.of(12, 20, 10).plus(123, ChronoUnit.MILLIS);
                final Message message = new Message(ZonedDateTime.of(date, time, ZoneId.systemDefault()));
        
                final String res = mapper.writeValueAsString(message);
        
                assertEquals("{\"dateTime\":\"2000-01-01T12:20:10.123\",\"zonedDateTime\":\"2000-01-01T12:20:10.123\"}", res);
            }
        
        }
    

    更新:

    如果您想集中配置它,您可以:

    1. 尝试将日期格式设置为您的 ObjectMapper,如 here 所述
    mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"));
    
    1. 自定义您的映射器,如here 所述
    @SpringBootApplication
    public class InstantSerializerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(InstantSerializerApplication.class, args);
        }
    
        @Bean
        public Jackson2ObjectMapperBuilderCustomizer addCustomTimeSerialization() {
            return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.serializerByType(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
                @Override
                public void serialize(ZonedDateTime zonedDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
                    jsonGenerator.writeString(formatter.format(zonedDateTime));
                }
            });
        }
    }
    

    【讨论】:

    • 是的,这行得通,但我希望集中配置它,而不是在我的所有可序列化类中注释所有 Instant 字段。
    • 回复已根据您的评论更新
    • 我说JsonFormat注解有效,因为我以前用过,但直到现在才真正尝试过。我收到 JSON 处理错误。我猜它不适用于 Instant。也许我会放弃并改用 ZonedDateTime。
    • SimpleDateFormat 永远不应该被使用。改用 DateTimeFormatter
    【解决方案2】:

    更好的方法是

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    private final ZonedDateTime zonedDateTime;
    

    有关更多信息,请参阅此question 接受的答案

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 2017-09-09
      • 2021-03-31
      • 2019-04-03
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      相关资源
      最近更新 更多