杰克逊不认识存在的领域

时间:2022-09-19 18:04:35

So here's my JSON

这是我的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:

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

Unrecognized field "totalSize"

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

但它确实存在于JSON和实体中!

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!

谢谢!

1 个解决方案

#1


1  

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

您正在使用Lombok。Jackson看不到您的getter和setter方法。

So you have two options:

所以你有两个选择:

  1. Do not use Lombok and implement the getter and setter methods
  2. 不要使用Lombok并实现getter和setter方法?
  3. Use Lombok with this additional library: jackson-lombok
  4. 使用Lombok附加库:jackson-lombok

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

如果您正在使用maven,那么在您的poml .xml中添加jackson-lombok:

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

Configure then your ObjectMapper in this way:

然后以这种方式配置ObjectMapper:

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

#1


1  

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

您正在使用Lombok。Jackson看不到您的getter和setter方法。

So you have two options:

所以你有两个选择:

  1. Do not use Lombok and implement the getter and setter methods
  2. 不要使用Lombok并实现getter和setter方法?
  3. Use Lombok with this additional library: jackson-lombok
  4. 使用Lombok附加库:jackson-lombok

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

如果您正在使用maven,那么在您的poml .xml中添加jackson-lombok:

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

Configure then your ObjectMapper in this way:

然后以这种方式配置ObjectMapper:

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