Jackson Not Overriding Getter with @JsonProperty

前端 未结 10 2084
醉话见心
醉话见心 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:29

    I had same proplem

    You need just to replace import import com.fasterxml.jackson.annotation.JsonProperty; on import org.codehaus.jackson.annotate.JsonProperty; Its work.

    0 讨论(0)
  • 2021-02-12 06:29

    I recently came across another interesting twist on this issue. We started using the Hibernate5Module to help with some lazy loading issues. Furthermore, we use Groovy so we are not defining getters and setters.

    It turns out that the Hibernate Module seems to interfere with the @JsonProperty annotation. Specifically if you have something annotated with @Transient So, if you have something like:

    @Transient
    @ApiModelProperty(required = true)
    @JsonProperty("alternateName")
    String property
    

    You won't see the alternateName in the JSON. Furthermore, your clients will likely have trouble with their POSTs and PUTs! To fix this, you can use a simple workaround. Define the getters and setters for the internal name you need to use(*) and don't use the value attribute on @JsonProperty So this works:

    @Transient
    @ApiModelProperty(required = true)
    @JsonProperty
    String alternateName
    
    void setProperty(String property) {
        this.alternateName = property
    }
    
    @JsonIgnore
    String getProperty() {
        this.alternateName
    }
    

    Note the use of the @JsonIgnore on the getter. If you don't, your framework will probably pick it up and you'll have duplicate entries for the same thing in your JSON.

    Anyhow - I'm hoping this helps someone!

    (*)We were trying to adhere to a particular interface, thus enforcing the name internally. However, the exposed API needed a different, user-friendly name.

    0 讨论(0)
  • 2021-02-12 06:30

    I was missing databind dependency

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${fasterxml.version}</version>
    </dependency>
    
    0 讨论(0)
  • 2021-02-12 06:36

    I know its an old question but for me I got it working when I figured out that its conflicting with Gson library so I had to use @SerializedName("name") instead of @JsonProperty("name") hope this helps

    0 讨论(0)
  • 2021-02-12 06:39

    The problem was that I was using both the old and new jackson libraries

    i.e. before I had import org.codehaus.jackson.annotate.JsonProperty; Which I had to change to below, to be consistent with the library I was using.

    Since I was using maven that also meant updating my maven dependencies. import com.fasterxml.jackson.annotation.JsonProperty;

    For it to work, I needed the @JsonProperty annotation on the getter (putting it on the object didn't work)

    I found the answer here (thanks to francescoforesti) @JsonProperty not working as expected

    0 讨论(0)
  • 2021-02-12 06:45

    Camel cases still seem to have issues even after defining proper annotations. Example:

    @JsonProperty("mirrorport") private String mirrorPort;

    Deserialization still fails when xml has <mirrorport>YES</mirrorport>

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