android – 无法为非具体的Collection类型找到反序列化
作者:互联网
我正在使用jackson库将JSON映射到对象中.我已经简化了很多问题,这就是发生的事情:
public class MyObject{
public ForeignCollection<MySecondObject> getA(){
return null;
}
public ForeignCollection<MyThirdObject> getB(){
return null;
}
}
我正在解析一个空的JSON字符串:
ObjectMapper mapper = new ObjectMapper();
mapper.readValue("{}", MyObject.class);
在readValue上,我得到了这个例外:
com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; class com.j256.ormlite.dao.ForeignCollection, contains [simple type, class com.test.MyThirdObject]]
当我在MyObject类中有两个返回ForeignCollection的get方法时会发生这种情况.删除其中一个get方法不会产生任何异常.
我实际上对映射器查看get方法的事实感到惊讶,它应该只设置我指示的字段.
这里发生了什么?
解决方法:
您需要在ObjectMapper中使用Guava模块.这是Maven的依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>{whatever is the latest}</version>
</dependency>
在你的代码中:
ObjectMapper mapper = new ObjectMapper();
// register module with object mapper
mapper.registerModule(new GuavaModule());
您可以省略@JsonDeserialize和@JsonSerialize注释.
更多信息here.
标签:android,json,jackson,ormlite 来源: https://codeday.me/bug/20191003/1847605.html