Jackson serializes a ZonedDateTime wrongly in Spring Boot

前端 未结 4 1844
有刺的猬
有刺的猬 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:10

    For Jackson 2.10 and above,

    parent pom.xml

    
    
      
        com.fasterxml.jackson
        jackson-bom
        2.10.3
        pom
        import
      
    
    

    module pom.xml

    
    
        com.fasterxml.jackson.datatype
        jackson-datatype-jsr310
    
    

    JsonMapper creation, possibly in your @Configuration class

    @Bean
    public JsonMapper jsonMapper() {
        return JsonMapper.builder()
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .addModule(new JavaTimeModule())
            .build();
    }
    

    Further reading:

    • What's new in Jackson 2.10
    • Use JsonMapper instead of ObjectMapper
    • Use Builder pattern

提交回复
热议问题