Jackson does not override Getter with @JsonProperty

JsonProperty does not override the default name that jackson receives from the receiver. If I serialize the class below using 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 a string does not work. The only way I can change the name is to rename the getter, the only problem is that it will always be lowercase for the first letter

+11
source share
11 answers

The problem was that I used both old and new Jackson libraries

i.e. before I import org.codehaus.jackson.annotate.JsonProperty; Which I had to modify below to fit the library I used.

Since I used maven, it also meant updating my maven dependencies. import com.fasterxml.jackson.annotation.JsonProperty;

For it to work, I need the @JsonProperty annotation on the getter (I couldn’t put it on the object)

I found the answer here (thanks francescoforesti) @JsonProperty is not working properly

+23
source

I had this problem when upgrading from an older version to 2.8.3 from FasterXML Jackson.

The problem was that when deserializing the JSON response from our database into a Java class object, our code did not have @JsonSetter in the class settings. Therefore, when serializing, the output did not use the class getters to serialize the Java class object into JSON (therefore, the @JsonProperty() decorator did not take effect).

I fixed the problem by adding @JsonSetter("name-from-db") to the setter method for this property.

In addition, instead of @JsonProperty() , to rename properties using the getter method, you can and should use @JsonGetter() , which is more specific to renaming properties.

Here is our code:

 public class KwdGroup { private String kwdGroupType; // Return "type" instead of "kwd-group-type" in REST API response @JsonGetter("type") // Can use @JsonProperty("type") as well public String getKwdGroupType() { return kwdTypeMap.get(kwdGroupType); } @JsonSetter("kwd-group-type") // "kwd-group-type" is what JSON from the DB API outputs for code to consume public void setKwdGroupType(String kwdGroupType) { this.kwdGroupType = kwdGroupType; } } 
+3
source

I had the same proplem

You just need to replace the import import com.fasterxml.jackson.annotation.JsonProperty; at import org.codehaus.jackson.annotate.JsonProperty; His job.

+2
source

I know this is an old question, but it worked for me when I found out that it conflicts with the Gson library, so I had to use @SerializedName("name") instead of @JsonProperty("name") hope this helps

+2
source

Camel cases still have problems even after identifying the correct annotations. Example:

@JsonProperty ("mirrorport") private String mirrorPort;

Desicialization is still not performed when xml has <mirrorport>YES</mirrorport>

+1
source

Put it on a variable, not on getter

 class JacksonTester { @JsonProperty("hello") private String hi; public String getHi() { return hi; } } 
0
source

Have you tried below

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

I mean, make the hi variable declared private. Alternatively, try putting @JsonIgnore in a variable declaration in case you prefer to store it by default.

0
source

Recently, I met another interesting twist on this issue. We started using the Hibernate5Module to help with some lazy loading problems. In addition, we use Groovy, so we do not define getters and setters.

It turns out that the Hibernate module seems to be interfering with the @JsonProperty annotation. In particular, if you have something annotated with @Transient So, if you have something like:

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

You will not see alternateName in JSON. In addition, your customers are likely to have problems with their POST and PUT! To fix this, you can use a simple workaround. Define getters and setters for the internal name you need to use (*), and do not 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 @JsonIgnore on the getter. If you do not, your infrastructure will most likely pick it up and you will have duplicate entries for the same thing in your JSON.

In any case - I hope this helps someone!

(*) We tried to stick to a specific interface, thereby applying the internal name. However, the open API requires a different, user-friendly name.

0
source

I had the same problem. The problem was the variable name. I made up my mind, and the first letter was β€œTop”, I just changed, and the problem was fixed.

changes:

 @JsonProperty(value="hello") private String MyVar; 

in

 @JsonProperty(value="hello") private String MyVar; 
0
source

I lacked data dependency

 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${fasterxml.version}</version> </dependency> 
0
source

When using Kotlin

I understand that the initial question is about Java, but as Kotlin is becoming very popular and many can use it, I would like to post it here to help others.

In any case, for Kotlin, because how getters / setters work, if you use val , that is, you only expose a getter, you may need to apply annotation to the getter, as shown below:

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

Source: https://habr.com/ru/post/1232527/


All Articles