MongoDB 模型的序列化类

问题描述 投票:0回答:8

当我向mongodb中插入一个List时,出现了一个问题:

Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class mongodb.Person
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
    at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:259)
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:198)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
    at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
    at com.mongodb.OutMessage.putObject(OutMessage.java:142)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:252)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:211)
    at com.mongodb.DBCollection.insert(DBCollection.java:57)
    at com.mongodb.DBCollection.insert(DBCollection.java:87)
    at com.mongodb.DBCollection.save(DBCollection.java:716)
    at com.mongodb.DBCollection.save(DBCollection.java:691)
    at mongodb.MongoDB.main(MongoDB.java:45)

Person类定义如下:

class Person{
    private String name;
    public Person(String name){
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

节目是:

        DBCollection coll = db.getCollection("test");
        DBObject record = new BasicDBObject();
        List<Person> persons= new ArrayList<Person>();
        persons.add(new Person("Jack"));
        record.put("person", persons);
        coll.save(record);

我无法从谷歌找到答案,所以请帮助我。

java mongodb mongo-java
8个回答
7
投票

只需在 Person 类中实现 Serializable 接口。

在你的课堂上定义一个

serialVersionUID
也是很好的。

AFAIK,在 java 中创建 POJO 类时,该类应该是可序列化的,如果它要通过某个流传输,具有默认构造函数,并允许使用 getter 和 setter 方法访问属性/字段。

您可能有兴趣阅读:发现 Java 序列化 API 的秘密


1
投票

我在使用 mongodb 时遇到了同样的异常。我尝试使有问题的类可序列化,但这并没有解决我的问题。

以下是对我有用的。 将类扩展为 BasicDBObject 的子类。当然,这只有在问题是由 MongoDB 引起的情况下才有效。

extends BasicDBObject 

原始出处

http://techidiocy.com/cant-serialize-class-mongodb-illegal-argument-exception/#comment-1298


0
投票

类 Person 应该实现

java.io.Serializable
接口。

class Person implements Serializable


0
投票

您的

Person
类定义需要具有
implements Serializable
才能对其进行序列化,例如:

class Person implements Serializable {
   //Rest here
}

这里有一些关于 Java 对象序列化的有用链接:Link, Link.


0
投票

这里的问题不在“implements Serializable”。 问题是对象没有在 DBObject 中转换。

可能的解决方案:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.*;

....

ObjectMapper mapper = new ObjectMapper();
DBObject dboJack = mapper.convertValue(new Person("Jack"), BasicDBObject.class);
...

0
投票

您可以使用以下代码实现此目的:

import com.google.gson.annotations.Expose;
import com.mongodb.ReflectionDBObject;

class PersonList extends ReflectionDBObject {
    // person property
    @Expose public java.util.List<Person> person;
}

现在在你的 mongodb 代码中,你可以序列化一个 Person 列表,如下所示

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

0
投票

这里是序列化 Employee 对象的代码示例:

public class Employee implements Serializable {

    private int empId;
    private String name;

    public int getEmpId() {
        return empId;
    }

    public String getName() {
        return name;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "EMployee id : " + empId + "  \nEmployee Name : " + name;
    }
}

//Another Main Class
public class Main{
    public static void main(String[] args) 
        throws FileNotFoundException, IOException, ClassNotFoundException {

        String filename = "data.txt";
        Employee e = new Employee();
        e.setEmpId(101);
        e.setName("Yasir Shabbir");

        FileOutputStream fos = null;
        ObjectOutputStream out = null;

        fos = new FileOutputStream(filename);
        out = new ObjectOutputStream(fos);
        out.writeObject(e);

        out.close();

        // Now to read the object from file
        // save the object to file
        FileInputStream fis = null;
        ObjectInputStream in = null;

        fis = new FileInputStream(filename);
        in = new ObjectInputStream(fis);
        e = (Employee) in.readObject();
        in.close();

        System.out.println(e.toString());
    }
}

-1
投票

首先你应该知道为什么要让class Serializable? 每当您想将网络上的对象移动到文件、数据库、网络、进程或任何其他系统时。 在java中简单实现。 只需实现可序列化接口。

© www.soinside.com 2019 - 2024. All rights reserved.