如何序列化类?

时间:2012-01-05 06:17:37

标签: java mongodb mongo-java

当我将一个List插入mongodb时,有一个问题:

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

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

8 个答案:

答案 0 :(得分:7)

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

最好在班级中定义serialVersionUID

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

您可能有兴趣阅读此内容:Discover the secrets of the Java Serialization API

答案 1 :(得分:1)

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

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

extends BasicDBObject 

原始资料

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

答案 2 :(得分:0)

class Person应该实现java.io.Serializable接口。

class Person implements Serializable

答案 3 :(得分:0)

您的Person类定义需要implements Serializable才能序列化,例如:

class Person implements Serializable {
   //Rest here
}

以下是Java对象序列化的一些有用链接:LinkLink

答案 4 :(得分: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());
    }
}

答案 5 :(得分:0)

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

可能的解决方案:

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

....

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

答案 6 :(得分: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代码中,您可以按如下方式序列化人员列表

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

答案 7 :(得分:-1)

首先你应该知道为什么要使用Serializable类? 每当您想要将网络上的obeject移动到文件,数据库,网络,进程或任何其他系统时。 在java中简单实现。 只需实现Serializable接口。

相关问题