【问题标题】:In spring boot java project how to map a property of different data types from a mongo DB to java class?在spring boot java项目中,如何将不同数据类型的属性从mongo DB映射到java类?
【发布时间】: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


    【解决方案1】:

    在我看来你有点不明白DAO类是什么。你有的是JavaBeans

    关于您的问题,正如您注意到的以及异常详细信息中报告的那样,您的文档是ArrayList,为什么不直接将其读取为ArrayList,然后将其设置为Section 字段? 但是如果这个选项不适合你,你可以实现Custom Converter,这个也在异常详情中报告给你。

    【讨论】:

    • 我不确定它是一个 ArrayList 数组(如格式 1 所示)还是一个对象数组(如格式 2 所示)
    • @user_531 那么是否值得将您的数据整理并以一种格式保存?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2012-10-08
    • 2021-06-26
    • 2016-08-18
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多