jackson annotations being ignored

前端 未结 2 1465
梦如初夏
梦如初夏 2021-01-03 00:35

I\'m trying to use Jackson annotations to re-name some of the json labels produced during serialization. The annotations all compile fine, and when I run, the Jackson seria

相关标签:
2条回答
  • 2021-01-03 00:44

    Just in case that somebody hits a similiar problem where only @JsonFormat gets ignored:

    Consider that in Spring Boot + Java 8 context the processing of LocalDateTime may experience troubles. Instead of following dependency:

     compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jdk8'
    

    You should use:

     compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310'
    

    Furthermore, when you create your Jackson ObjectMapper you need to register the implementation which treats LocalDateTime correctly:

     json = new ObjectMapper().findAndRegisterModules().writeValueAsString(entity);
     new ObjectMapper().findAndRegisterModules().readValue(json, entity.getClass());
    
    0 讨论(0)
  • 2021-01-03 00:59

    One relatively common reason is trying to use "wrong" set of annotations: Jackson 1.x and Jackson 2.x annotations live in different Java packages, and databind has to match major version. This design has the benefit of allowing 1.x and 2.x versions to be used side by side, without class loading conflicts; but downside that you have to make sure that you have matching versions.

    Biggest problem is the use by frameworks: many JAX-RS implementations (like Jersey) still use Jackson 1.x by default. So I am guessing you might be using Jackson 1.x indirectly, but adding Jackson 2.x annotations. If so, you need to use 1.x annotations (ones under org.codehaus.jackson) instead.

    0 讨论(0)
提交回复
热议问题