why are these Jackson annotations necessary?

djna

I am making a REST call and using Jackson to parse the response.

In cases where JSON attributes and Java field names don't match I need supply a @JsonProperty annotation. However in other applications I've written with earlier versions of Jackson, where JSON and Java match no annotation is needed.

@JsonIgnoreProperties(ignoreUnknown = true)
public class Location {

    @JsonProperty(value="i")
    String id;

    // unless I include this annotation the name is not deserialised.
    @JsonProperty(value="name")
    String name;
    
    // etc.
}

Why would I need to specify that seemingly redundant mapping?

I am using Maven and my effective pom shows

 <jackson-bom.version>2.12.4</jackson-bom.version>

I specify

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.32</version>
</dependency>
DVLPR

Jackson looks for attributes based on the getters of the class and not by its fields unless they are explicitly annotated.

Define a String getName() method and you won't need the annotation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related