好的,首先这是从我的 Web 服务返回的 JSON。在我的 Android ContentProvider 中的 ResponseHandler 中进行异步查询后,我试图将其反序列化为 pojos。
{"exampleList" : [{
"locationId" : "00001" ,
"owners" : [
{
"paidID" : { "$oid" : "50a9c951300493f64fbffdb6"} ,
"userID" : { "$oid" : "50a9c951300493f64fbffdb6"}
} ,
{
"paidID" : { "$oid" : "50a9c951300493f64fbffdb7"} ,
"userID" : { "$oid" : "50a9c951300493f64fbffdb7"}
}
]
}]}
起初,我对所看到的问题感到困惑,因为我在我的 Web 服务中使用了与我在 Android 应用程序中相同的 Jackson 注释 bean——但后来我意识到该owners
对象从未在示例 JSON 中发送到我的 Web 服务(它跳过我的 Web 服务上的 POJO,并通过来自 DAO 的原子更新添加到 mongoDB 中的文档中)。
那么好吧。到目前为止,Jackson 不必处理该owners
对象,现在它被它窒息了,即:
JsonMappingException
:无法反序列化[char position where you can find and ] 通过引用链 [包含所有者类的我的 Jackson bean 的路径]处java.lang.String
的 令牌实例START_OBJECT
"userID"
"paidID"
我的 Jackson bean 有一个包装器,这就是“exampleList”的全部内容:
public class Examples extends HashMap<String, ArrayList<Example>> {
}
然后是实际的Example
课程:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Example implements Comparable<Example> {
@ObjectId @Id
private String id;
@JsonProperty(Constants.Example.location)
private String location;
@JsonProperty(Constants.Example.OWNERS)
private List<Owners> owners;
public int compareTo(Example _o) {
return getId().compareTo(_o.getId());
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public List<Example.Owners> getOwners() {
return owners;
}
public void setOwners(List<Example.Owners> owners) {
this.owners = owners;
}
public Example() {
}
@JsonCreator
public Example(@Id @ObjectId String id) {
this.id = id;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Owners implements Comparable<Owners> {
@JsonProperty(Constants.Example.USERID)
private String userID;
@JsonProperty(Constants.Example.PAIDID)
private String paidID;
public Owners() {
}
public int compareTo(Owners _o) {
return getUserID().compareTo(_o.getUserID());
}
@ObjectId
public String getUserID() {
return userID;
}
@ObjectId
public void setUserID(String userID) {
this.userID = userID;
}
@ObjectId
public String getPaidID() {
return paidID;
}
@ObjectId
public void setPaidID(String paidID) {
this.paidID = paidID;
}
}
}
最后,ResponseHandler 中的代码全部失败(第二行产生 JsonMappingException):
objectMapper = MongoJacksonMapperModule.configure(objectMapper);
mExamples = objectMapper.readValue(jsonParser, Examples.class);
我有一种感觉,问题是杰克逊仍然不知道如何映射那些$oid
mongoDB ObjectIds。MongoJacksonMapper 库应该通过提供@ObjectId
注释和配置 ObjectMapper 以使用该库的方法来帮助实现这一点,但它仍然无法正常工作。出于某种原因,它仍在寻找作为字符串的 userID 或paidID,而不是 ObjectId。有任何想法吗?