这是我的 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 时,出现以下错误:
Unrecognized field "totalSize"
但它肯定同时存在于 JSON 和实体中!
下面是我写的将字符串映射到实体的代码,供引用:
EsidesiEntity apexJobResponseEntity;
ObjectMapper apexMapper = new ObjectMapper();
try {
apexJobResponseEntity = apexMapper.readValue(apexResponseString, EsidesiEntity.class);
} ...
我是否遗漏了一些非常基本的东西?
(顺便说一句,如果类/实体名称中存在一些不一致,那是因为我在将它们发布到网上之前重命名了它们。让我知道,我会在看到它们时修复它们。)
谢谢!
最佳答案
您正在使用 Lombok . Jackson 看不到您的 getter 和 setter 方法。
所以你有两个选择:
- 不使用 Lombok 并实现 getter 和 setter 方法
- 将 Lombok 与此附加库一起使用:jackson-lombok
如果您使用的是 maven,那么将 jackson-lombok 添加到您的 pom.xml 中:
<dependency>
<groupId>com.xebia</groupId>
<artifactId>jackson-lombok</artifactId>
<version>1.1</version>
</dependency>
然后以这种方式配置您的 ObjectMapper
:
ObjectMapper apexMapper = new ObjectMapper();
apexMapper.setAnnotationIntrospector(new JacksonLombokAnnotationIntrospector());
[...]
关于java - jackson 不承认存在的领域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39779219/