Skip to content
Advertisement

Jackson Not Overriding Getter with @JsonProperty

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

{"hi":"hello"}

As you can see the JsonProperty annotation has no effect

class JacksonTester {
    String hi;

    @JsonProperty("hello")
    public String getHi() {
        return hi;
    }
}   

Putting @JsonProperty on the String itself doesn’t work either. The only way it seems that I can change the name is by renaming the getter, the only problem is that it then will always be lowercase for the first letter

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement