如何序列化类?

当我将一个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 persons= new ArrayList(); persons.add(new Person("Jack")); record.put("person", persons); coll.save(record); 

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

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

最好在您的类中定义serialVersionUID

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

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

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

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

 extends BasicDBObject 

原始来源

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

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

class Person implements Serializable

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

 class Person implements Serializable { //Rest here } 

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

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

可能的方法:

 import com.fasterxml.jackson.databind.ObjectMapper; import com.mongodb.*; .... ObjectMapper mapper = new ObjectMapper(); DBObject dboJack = mapper.convertValue(new Person("Jack"), BasicDBObject.class); ... 

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

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

现在在您的mongodb代码中,您可以按如下方式序列化Person列表

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

以下是使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()); } } 

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