好的,首先这里是从我的web服务返回的JSON。我试图在我的Android ContentProvider中的ResponseHandler中进行异步查询后将其反序列化为pojos。杰克逊JSON反序列化MongoDB ObjectId
{"exampleList" : [{
"locationId" : "00001" ,
"owners" : [
{
"paidID" : { "$oid" : "50a9c951300493f64fbffdb6"} ,
"userID" : { "$oid" : "50a9c951300493f64fbffdb6"}
} ,
{
"paidID" : { "$oid" : "50a9c951300493f64fbffdb7"} ,
"userID" : { "$oid" : "50a9c951300493f64fbffdb7"}
}
]
}]}
起初,我很困惑,我所看到的问题,因为我用的是相同的杰克逊标注豆我的web服务,我在我的Android应用程序做的 - 但后来我意识到owners
对象从来没有发送过样本JSON到我的Web服务(它跳过我的Web服务中的POJO,并通过DAO中的原子更新添加到mongoDB中的文档中)。
好吧。截至目前,杰克逊不必处理owners
对象,而现在它是被窒息在其上,即:
JsonMappingException
:在[字符无法反序列化的实例java.lang.String
出START_OBJECT
令牌位置,你可以找到"userID"
和"paidID"
] 通过参考链[路径到我的杰克逊豆含有业主类]
我的杰克逊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的代码中,这是所有失败(2号线生产的JsonMappingException):
objectMapper = MongoJacksonMapperModule.configure(objectMapper);
mExamples = objectMapper.readValue(jsonParser, Examples.class);
我有一种感觉,问题是杰克逊仍然不知道如何映射那些是mongoDB ObjectIds的$oid
。 MongoJacksonMapper库应该可以通过提供@ObjectId
注释和一种配置ObjectMapper来使用该库的方法来提供帮助,但它仍然不起作用。出于某种原因,它仍然在寻找userID或paidID作为字符串,而不是ObjectId。有任何想法吗?
问题是关于反序列化,系列化没有。这个答案应该被下调。 – Arny