【发布时间】:2018-09-06 07:15:42
【问题描述】:
您好,我有一个 mongo DB 文档部分,其列属性如图所示(第一种格式)
"columns" : [
[
{
"itemId" : ObjectId("5b863b50083ae5eb1e678d75"),
"type" : "field"
}
],
[
{
"itemId" : ObjectId("5b8d4404af0963f54e262f46"),
"type" : "field"
}
],
[
],
[
]
]
which is of the type Array of Array of Objects
However at some places it is also stored in this format as well . (2nd format)
"columns" : [
{
"0" : {
"itemId" : "5b863b50083ae5eb1e678d75",
"type" : "field"
}
},
{
"0" : {
"itemId" : "5b8d4404af0963f54e262f46",
"type" : "field"
}
},
{
},
{
}
]
作为对象的对象数组
现在我有 dao 类 someObject 来存储最里面的对象
public class SomeObject{
private ObjectId itemId;
private String type;
public ObjectId getItemId() {
return itemId;
}
public void setItemId(ObjectId itemId) {
this.itemId = itemId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
这里是节道类
public class Section{
private List<List<SomeObject>> columns;
public List<List<someObject>> getColumns() {
return columns;
}
public void setColumns(List<List<SomeObject>> columns) {
this.columns = columns;
}
}
这对于第一种格式如何工作得很好,因为我将类型作为列表
但因第二种格式不同而中断
我也尝试过使用这个类
Public class Section {
private List<Object> columns;
}
这会映射第二种格式,但会中断第一种格式 我收到以下错误
"exceptionDetails": "Cannot convert [Document{{itemId=5877f2345449aef957e1d8ec, type=field}}] of type class java.util.ArrayList into an instance of class java.lang.Object! Implement a custom Converter<class java.util.ArrayList, class java.lang.Object> and register it with the CustomConversions.
请任何人都可以建议我如何通过 dao 类创建以便它可以映射两种格式?我需要实现自定义映射器吗?如果是,那怎么办?
【问题讨论】:
标签: java arrays mongodb objectmapper