nha*_*man 3 android json jackson ormlite
我正在使用 jackson 库将 JSON 映射到对象。我已经将问题简化了很多,这就是发生的情况:
public class MyObject{
public ForeignCollection<MySecondObject> getA(){
return null;
}
public ForeignCollection<MyThirdObject> getB(){
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在解析一个空的 JSON 字符串:
ObjectMapper mapper = new ObjectMapper();
mapper.readValue("{}", MyObject.class);
Run Code Online (Sandbox Code Playgroud)
在 上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]]
Run Code Online (Sandbox Code Playgroud)
当我在类中有两个 返回. 删除其中一种方法不会导致任何异常。getMyObjectForeignCollectionget
实际上,我对映射器查看get方法这一事实感到惊讶,它应该只设置我指示的字段。
这里发生了什么?
您需要在 ObjectMapper 中使用 Guava 模块。这是 Maven 依赖项:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>{whatever is the latest}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在你的代码中:
ObjectMapper mapper = new ObjectMapper();
// register module with object mapper
mapper.registerModule(new GuavaModule());
Run Code Online (Sandbox Code Playgroud)
您可以省略@JsonDeserialize和@JsonSerialize注释。
更多信息请点击这里。
| 归档时间: |
|
| 查看次数: |
17515 次 |
| 最近记录: |