2013-04-20 15 views
2

我簡單的應用程序是假設只是填充的MongoDB一起user_id之間的映射。我的密鑰(_id)採用JSON格式,其值很長。MongoDB中的Java和字符串不能序列類編碼

{ 
    "_id": { 
     "a": "1B2ac", 
     "b": "Windows NT 5.2; WOW64; rv:16.0 Ff/6.0" 
    }, 
    "user": 1999129 
} 

我有幾個問題:

問題1:我得到can's serialize class...當我嘗試插入:

Caused by: java.lang.IllegalArgumentException: can't serialize class test.mongo.foo.DummyObject 
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:234) 
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:121) 
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:86) 
    at com.mongodb.OutMessage.putObject(OutMessage.java:190) 
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:253) 
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:217) 
    at com.mongodb.DBCollection.insert(DBCollection.java:102) 
    at  com.test.mongo.foo.DaoImpl.insertRecords(DaoImpl.java:130) 

這裏是我的DummyObject類(實現Serializable):

package test.mongo.foo; 

import java.io.Serializable; 

public class DummyObject implements Serializable{ 

    private static final long serialVersionUID = -2715467675581503964L; 

    //default constructor 
    public DummyObject(){ 

    } 

    private String a; 
    private String b; 

    public String getA() { 
     return a; 
    } 

    public String getB() { 
     return b; 
    } 

    public void setA(String a) { 
     this.a = a; 
    } 

    public void setB(String b) { 
     this.b = b; 
    } 

} 

這是我的DaoImp

package test.mongo.foo; 
public class DaoImpl extends MongoDAOImpl{ 

    public int insertRecords(List<DBObject> records) { 
     DBCollection coll = getDBCollection(); 

     Exception e = null; 
     try { 
      WriteResult res = coll.insert(records); 
      if (res.getError() != null) { 
       throw new MongoRuntimeException(res.getError()); 
      } 
      return res.getN(); 
     } catch (Exception err) { 
      e = err; 
      throw new MongoRuntimeException(err); 
     } 
    } 

    public static DBObject convertToDBObject(DummyObject obj, long value) { 
     DBObject bdbo = new BasicDBObject(); 
     bdbo.put("_id",obj); 
     bdbo.put("user",value); 
     return bdbo; 
    } 
    public static void main(String [] args){ 
      DummyObject d = new DummyObject(); 
      d.setA("1B2ac"); 
      d.setB("Windows NT 5.2; WOW64; rv:16.0 Ff/6.0"); 
      long val = 1999129; 
      List<DBObject> l = new ArrayList<DBObject>(); 
      l.add(DaoImpl.convertToDBObject(d,val)); 
      DaoImpl impl = new DaoImpl(); 
    } 
} 

ISSUE 2:爲了避免上述問題,我試圖與串來串映射插入記錄。儘管(對_id)我輸入的字符串不是逃脫,DBObject內部逃脫,並最終寫入MongoDB的,如下:

{ 
    "_id": { 
     \"a\": \"1B2ac\", 
     \"b\": \"WindowsNT5.2;WOW64;rv: 16.0Ff/6.0\" 
    }, 
    "user": NumberLong(1999129) 
} 

INSTEAD OF

{ 
    "_id": { 
     "a": "1B2ac", 
     "b": "Windows NT 5.2; WOW64; rv:16.0 Ff/6.0" 
    }, 
    "user": 1999129 
} 

如何解決這兩個有什麼建議問題是什麼?任何指針都非常感謝。

回答

2

沒關係。在玩過一段時間後,我找到了自己的問題的答案。作爲JsonObject插入。我將我的DummyObject轉換爲JsonObject/Object(提示:Json.parse(jstr))

謝謝 Masti