2012-01-05 54 views
10

当我插入一个列表的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); 

我可以从谷歌找不到答案,请帮助我。

+0

你是怎么做到这一点的@vienna .. Plz帮助我我有同样的问题。 Plz帮助 – shalki 2012-06-22 08:00:11

+0

一个观察:MongoDB的好处之一是能够随着时间推移演变架构,而不必使用这些新字段更新现有文档。因此,您可能希望将对象存储为文档(例如用于名称的字段等),或者如果您确实想要执行二进制序列化,您可能更愿意使用诸如Google协议缓冲区之类的内容,这是更具前瞻性的实现二进制序列化的方式。 – Rich 2013-08-21 09:21:02

回答

7

只是在Person类中实现了Serializable接口。

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

您可能会感兴趣阅读本:Discover the secrets of the Java Serialization API

+1

但我仍然有问题,即使我实现了Serializable接口; class Person实现Serializable {private String name; \t public person(String name){ \t \t this.name = name; \t} \t public String getName(){ \t \t return name; \t} \t public void setName(String name){ \t \t this.name = name; \t} } – NOrder 2012-01-05 06:59:30

+0

@vienna:你是否得到相同的异常或其他? – 2012-01-05 07:01:22

+0

是的,我得到了相同的异常 – NOrder 2012-01-05 07:08:44

0

Person类应实现java.io.Serializable接口。

class Person implements Serializable

0

Person类定义需要有implements Serializable,以便它被序列化,例如:

class Person implements Serializable { 
    //Rest here 
} 

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

-1

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

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 
    pubic 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"); 
     enter code here 
      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()); 
     } 
    } 
} 
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); 
... 
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