Java – Jackson did not cover the aspirator with @ jsonproperty

Jsonproperty does not override the default name obtained from getter If I serialize the following classes with objectmapper and Jackson, I get

{"hi":"hello"}

As you can see, the jsonproperty annotation is invalid

class JacksonTester {
    String hi;

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

Putting @ jsonproperty on the string itself does not work It seems that the only way I can change the name is to rename getter. The only problem is that its first letter is always lowercase

Solution

The problem is that I'm using old and new Jackson libraries

Before me, import org codehaus. jackson. annotate. JsonProperty; I must change to the following in order to be consistent with the library I use

Because I use Maven also means updating my Maven dependencies import com. fasterxml. jackson. annotation. JsonProperty;

For it to work, I need the @ jsonproperty annotation on the getter (placing it on an object doesn't work)

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>