2012-01-05 60 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