how to serialize class?

2019-02-16 14:32发布

When I insert a List into mongodb, there is a problem:

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)

the class Person is defined as follows:

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

The program is :

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

I can't find the answer from google, so please help me.

8条回答
Lonely孤独者°
2楼-- · 2019-02-16 15:02

class Person should implement java.io.Serializable interface.

class Person implements Serializable

查看更多
欢心
3楼-- · 2019-02-16 15:02

First of all you should know why you make class Serializable? Whenever you want to move obeject on network to a file, database, network, process or any other system. In java simple Implementation. Just Implement Serializable interface.

查看更多
再贱就再见
4楼-- · 2019-02-16 15:05

Just implement Serializable interface in Person class.

Also it will be good to define a serialVersionUID in your class.

AFAIK, while creating POJO class in java, the class should be serializable, if it is going to be transfered over some stream, have a default constructor, and allows access to properties/fields using getter and setter methods.

You might be interested in reading this: Discover the secrets of the Java Serialization API

查看更多
beautiful°
5楼-- · 2019-02-16 15:06

I got the same exception while working with mongodb. I tried making the problematic class serializable but that didn't fix my problem.

Following is what worked for me. Extend the class to be a child of BasicDBObject . Of course this works only if the problem is caused by MongoDB.

extends BasicDBObject 

Original source

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

查看更多
贪生不怕死
6楼-- · 2019-02-16 15:09

The problem here not in "implements Serializable". The problem is that object not converted in the DBObject.

Possible solution:

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

....

ObjectMapper mapper = new ObjectMapper();
DBObject dboJack = mapper.convertValue(new Person("Jack"), BasicDBObject.class);
...
查看更多
叼着烟拽天下
7楼-- · 2019-02-16 15:12

Your Person class definition needs to have implements Serializable in order for it to be serialized, e.g.:

class Person implements Serializable {
   //Rest here
}

Here are some useful links on Java object serialization: Link, Link.

查看更多
登录 后发表回答