Jackson Not Overriding Getter with @JsonProperty

前端 未结 10 2126
醉话见心
醉话见心 2021-02-12 06:23

JsonProperty isn\'t overriding the default name jackson gets from the getter. If I serialize the class below with ObjectMapper and jackson I get

<
10条回答
  •  春和景丽
    2021-02-12 06:48

    If using Kotlin

    I understand the original question is in Java, but since Kotlin is becoming very popular and many might be using it I'd like to post this here to help others.

    Anyway, for Kotlin, because how getters/setters work, if you are using val, meaning you only expose the getter, you may need to apply the annotation to the getter like below:

    class JacksonTester(@get:JsonProperty("hello") val hi: String)
    
    

提交回复
热议问题