我有一个JSON
{
"warnings": {},
"item": {
"@class": "com.somePackage.model.SomeObject",
//some fields...
"comments": [
],
"otherObjects": [{
"@class": "com.somePackage.model.ObjectSmt",
//some other fields...
}]
}
}
我想将其反序列化为ItemResponse.class
public class ItemResponseDto {
private Map<String, List<String>> warnings;
private AbstractItem item;
public ItemResponseDto() {
this.warnings = new HashMap();
}
public ItemResponseDto(AbstractItem item) {
this.item = item;
this.warnings = new HashMap();
}
public AbstractItem getItem() {
return this.item;
}
public void setItem(AbstractItem item) {
this.item = item;
}
public Map<String, List<String>> getWarnings() {
return this.warnings;
}
public void setWarnings(Map<String, List<String>> warnings) {
this.warnings = warnings;
}
}
AbstractItem:
@JsonTypeInfo(
use = Id.CLASS,
include = As.PROPERTY,
property = "@class")
@JsonIgnoreProperties(
ignoreUnknown = true)
public abstract class AbstractItem implements Serializable{...
当我试图反序列化时:
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS)
.readValue(response.asString(), ItemResponseDto.class);
我得到了这个例外:
意外的令牌(START_OBJECT),预期的START_ARRAY:需要JSON数组来包含类java.util.Map的As.WRAPPER_ARRAY类型信息
好的,我可以摆脱警告,但是当我试图反序列化对象时
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS, JsonTypeInfo.As.PROPERTY)
.readValue(response.asString(), ItemResponseDTO.class)
我也得到错误:
意外的令牌(END_ARRAY),预期的VALUE_STRING:需要包含类型id的JSON字符串(对于java.util.List的子类型)
路径:com.somePackage.model.ItemResponseDTO [&#34; item&#34;]
路径:com.somePackage.model.SomeObject [&#34;评论&#34;]
我猜是因为评论字段不包含@class字段我收到此错误
那么,有什么方法可以忽略这个消息或smt吗?即使我尝试仅解析对象,我也会收到错误,指的是评论字段。我只有一个编译的代码,所以我无法编辑它。