简体   繁体   English

在Spring Boot Java项目中,如何将不同数据类型的属性从Mongo DB映射到Java类?

[英]In spring boot java project how to map a property of different data types from a mongo DB to java class?

Hi i have a mongo DB ducument section which has the columns property as shown (1st format) 嗨,我有一个mongo DB ducument部分,其栏属性如图所示(第一种格式)

   "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"
                }
            }, 
            {

            }, 
            {

            }
        ]

As Array of Object of Object 作为对象的对象数组

now i have dao class someObject to store the inner most object 现在我有道类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;
    }

}

here is the section Dao class 这是道课

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;
    }
}

how section class ever this works fine for first format as i have taken type as List 截面类如何在第一种格式下正常工作,因为我将类型设为列表

but breaks for second format as it has diffeferent 但由于格式不同而无法使用第二种格式

i also tried using this class 我也尝试过使用这个课程

Public class Section {

private List<Object> columns;

}

this mapps the second format but breaks for first format i got the following error 这映射了第二种格式,但是破坏了第一种格式,我得到以下错误

 "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.

please can anyone suggest how do i create by dao class such that it can map both the formats ?? 请任何人可以建议我如何创建dao类,以便它可以映射两种格式? do i need to implement custom mapper ? 我需要实现自定义映射器吗? if yes then how ? 如果是,那怎么办?

It seems to me you are a little do not understand what is DAO class. 在我看来您有点不了解什么是DAO类。 What you have are JavaBeans . 您拥有的是JavaBeans

And about your question, as you noticed and as reported in the exception details your document is an ArrayList , why not just read it as an ArrayList and then set it to Section field? 关于您的问题,正如您所注意到并在异常详细信息中所报告的那样,您的文档是ArrayList ,为什么不只是将其读取为ArrayList并将其设置为Section字段呢? But if this option does not suit you, you can implement Custom Converter , this is also reported to you in the exception details. 但是,如果此选项不适合您,则可以实现Custom Converter ,这也会在异常详细信息中报告给您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM