Jackson serializes a ZonedDateTime wrongly in Spring Boot

前端 未结 4 1850
有刺的猬
有刺的猬 2021-02-05 01:28

I have a simple application with Spring Boot and Jetty. I have a simple controller returning an object which has a Java 8 ZonedDateTime:

public clas         


        
4条回答
  •  野的像风
    2021-02-05 02:13

    There is a library jackson-datatype-jsr310. Try it.

    This library covers new datetime API and includes serializers for ZonedDateTime too.

    All you need is just to add JavaTimeModule:

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    

    UPDATE

    To convert datetime to ISO-8601 string you should disable WRITE_DATES_AS_TIMESTAMPS feature. You can easily do by either overriding ObjectMapper bean or by using application properties:

    spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
    

提交回复
热议问题