Narwhal
Narwhal

Reputation: 335

Jackson not recognizing fields that exist

So here's my JSON

{"totalSize":46,"done":true,"records":[{"Name":"Wamu I","Start_Date__c":"2016-09-26T16:56:10.000+0000","Status__c":"Completed","Type__c":"Your were expecting success, but In reality it was I, Dio!!!"}]}

And here are my two entity classes:

@JsonIgnoreProperties(ignoreUnknown = true)
public class EsidesiJobEntity {

    @JsonProperty("totalSize")
    private @Getter @Setter Integer totalSize;

    @JsonProperty("done")
    private @Getter @Setter Boolean isDone;

    @JsonProperty("records")
    private @Getter @Setter List<KarsEntity> records;

    @Override
    @JsonIgnore
    public String toString(){

        List<String> recordsObjectString = new ArrayList<String>();

        this.records.forEach((record) -> 
        {
            recordsObjectString.add(record.toString());
        });
        return "{ totalSize:"+this.totalSize+", isDone:"+this.isDone+", records:["+recordsObjectString.toString()+"]";
    }

}

@JsonIgnoreProperties(ignoreUnknown = true)
public class KarsEntity {

    @JsonProperty("Name")
    private @Getter @Setter String name;

    @JsonProperty("Start_Date__c")
    private @Getter @Setter String startDate;

    @JsonProperty("Status__c")
    private @Getter @Setter String status;

    @Override
    public String toString(){
        return "{ name:"+this.name+", startDate:"+this.startDate+", status:"+this.status+"}";
    }
}

for some reason, when I map that json string to the EsidesiJobEntity, I get the following error:

Unrecognized field "totalSize"

BUT IT DEFINITELY EXISTS IN BOTH IN BOTH THE JSON AND THE ENTITY!

Here's the code I wrote to map the string to the entity for reference:

EsidesiEntity apexJobResponseEntity;

ObjectMapper apexMapper = new ObjectMapper();
try {
    apexJobResponseEntity = apexMapper.readValue(apexResponseString, EsidesiEntity.class);
} ...

Am I missing something really basic?

(BTW, If there's some inconsistency in the Class/Entity names, its because I renamed them before posting them online. Let me know and I'll fix them as I see them. )

Thanks!

Upvotes: 3

Views: 3730

Answers (2)

מרים בלוך
מרים בלוך

Reputation: 11

Try this:

Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(youJson);
MyPojo pojo = gson.fromJson(jsonElement, MyPojo.class);

Upvotes: 0

devops
devops

Reputation: 9179

You are using Lombok. Jackson can't see your getter and setter methods.

So you have two options:

  1. Do not use Lombok and implement the getter and setter methods
  2. Use Lombok with this additional library: jackson-lombok

If you are using maven, so add jackson-lombok to your pom.xml:

<dependency>
    <groupId>com.xebia</groupId>
    <artifactId>jackson-lombok</artifactId>
    <version>1.1</version>
</dependency>

Configure then your ObjectMapper in this way:

ObjectMapper apexMapper = new ObjectMapper();
apexMapper.setAnnotationIntrospector(new JacksonLombokAnnotationIntrospector());
[...]

Upvotes: 1

Related Questions