我有一个带委托(delegate)的类(class)设置
public class MyClass implements List<Integer> {
public String name;
public List<Integer> target; // this is the delegation target
// more fields
@Override
public Integer get(int index) {
return target.get(index);
}
// all other method in target interface is delegated
}
我得到了一个如下所示的 JSON:
{"target": [1, 2, 3] , "name":"foo"}
jackson 抛出了这个:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.foo.MyClass out of START_OBJECT token
at [Source: java.io.StringReader@156e5f3e; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:256)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:214)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2986)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2091)
我推测 Jackson 认为 MyClass 是一个列表,所以不知道如何处理 {}
因为[]
预计
我通过制作 MyClass
证实了我的猜测不实现List<Integer>
:
public class MyClass { /*same stuff*/}
一切正常。但是我需要 MyClass 来实现 List<Integer>
....
我可以使用模块中的注释或配置来解决这个问题吗?
最佳答案
我在阅读 this article 时偶然发现了我的答案, 基本上,我需要用
注释我的类@JsonFormat(shape = Shape.OBJECT)
关于java - Jackson JSON强制反序列化为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27728503/