杰克逊没有认识到存在的领域 [英] Jackson not recognizing fields that exist

查看:107
本文介绍了杰克逊没有认识到存在的领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的JSON

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!!!"}]}

这是我的两个实体类:

@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+"}";
    }
}

出于某种原因,当我将json字符串映射到EsidesiJobEntity,我收到以下错误:

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

Unrecognized field "totalSize"

但它绝对同时存在于JSON和ENTITY中!

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

这是代码I写道将字符串映射到实体以供参考:

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. )

谢谢!

推荐答案

您正在使用< a href =https://projectlombok.org/features/GetterSetter.html =nofollow>龙目岛。杰克逊看不到你的getter和setter方法。

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

所以你有两个选择:


  1. 不要使用Lombok并实现getter和setter方法

  2. 将Lombok与此附加库一起使用: jackson-lombok

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

如果你正在使用maven,那么添加 jackson -lombok 到你的pom.xml:

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>

以这种方式配置你的 ObjectMapper

Configure then your ObjectMapper in this way:

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

这篇关于杰克逊没有认识到存在的领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆