我试图反序列化一个巨大的API有效负载。这个有效负载包含的字段比我需要的多得多,因此我正在使用@JsonIgnoreProperties(ignoreUnknown = true)
。但是,在某个时候反序列化失败了,错误消息是:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of FIELD_NAME token
at [Source: {
"objectEntries": [
{
"objectKey": "KDS-4300"
},
{
"objectKey": "KDS-4327"
}
]
}; line: 2, column: 3]
我找到了那个案子的解决方案,建议使用
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
我试过这个。但这没什么用。而且,我的结果数据不是单个值数组。它实际上包含两个值--因此解决方案无论如何都不会加起来。
下面是反序列化的目标类。
@JsonIgnoreProperties(ignoreUnknown = true)
public class InsightQueryResult {
@JsonProperty("objectEntries")
private List<ObjectEntry> objectEntries;
@JsonCreator
public InsightQueryResult(List<ObjectEntry> objectEntries) {
this.objectEntries = objectEntries;
}
public List<ObjectEntry> getObjectEntries() {
return objectEntries;
}
// equals, hashCode and toString
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class ObjectEntry {
@JsonProperty("objectKey")
private String objectKey;
@JsonCreator
public ObjectEntry(String objectKey) {
this.objectKey = objectKey;
}
public String getObjectKey() {
return objectKey;
}
// equals, hashCode and toString
}
下面是我测试它的单元测试:
@Test
public void shouldMapQueryResultToResultObject() throws IOException {
final Resource expectedQueryResult= new ClassPathResource("testQueryPayload.json");
final String expectedQueryResultData = new String(
Files.readAllBytes(expectedQueryResult.getFile().toPath())).trim();
final List<ObjectEntry> objectEntries = Arrays.asList(new ObjectEntry("KDS-4300"), new ObjectEntry("KD-4327"));
final InsightQueryResult expectedQueryResult = new InsightQueryResult(objectEntries);
final InsightQueryResult result = objectMapper.readValue(expectedQueryResultData, InsightQueryResult.class);
assertThat(result).isEqualTo(expectedQueryResult);
}
下面是我想反序列化的有效载荷
// testQueryPayload.json
{
"objectEntries": [
{
"objectKey": "KDS-4300"
},
{
"objectKey": "KDS-4327"
}
]
}
发布于 2021-12-08 13:54:54
您应该简单地注释@JsonCreator
的参数。
@JsonCreator
public ObjectEntry(String objectKey) {
this.objectKey = objectKey;
}
变成了
@JsonCreator
public ObjectEntry(@JsonProperty(value = "objectKey", required = true) String objectKey) {
this.objectKey = objectKey;
}
其他构造函数也是如此。
解释:
@JsonProperty("name")
(或带有@JsonValue
的getter )注释一个字段,这允许reflection.@JsonCreator
注释一个构造函数时,您告诉杰克逊这是他们应该使用的构造函数来从Json字符串构建对象。但是,要么给他们一个空的构造函数(然后通过反射,他们以后会设置每个字段),要么告诉他们必须在每个构造函数参数中使用Json字符串的哪个字段。https://stackoverflow.com/questions/70275800
复制相似问题