how to serialize class?

后端 未结 8 1929
自闭症患者
自闭症患者 2021-01-11 19:28

When I insert a List into mongodb, there is a problem:

Exception in thread \"main\" java.lang.IllegalArgumentException: can\'t serialize class mongodb.Person         


        
8条回答
  •  被撕碎了的回忆
    2021-01-11 20:12

    You can achieve this by using following code:

    import com.google.gson.annotations.Expose;
    import com.mongodb.ReflectionDBObject;
    
    class PersonList extends ReflectionDBObject {
        // person property
        @Expose public java.util.List person;
    }
    

    Now in your mongodb code, you can serialise a Person list as follows

    ....
    PersonList personList = new PersonList();
    personList.person = new ArrayList<>();
    // add persons to the list
    ....
    ....
    record.put("personsList", personList);
    ....
    // rest of your code
    

提交回复
热议问题