how to serialize class?

后端 未结 8 1925
自闭症患者
自闭症患者 2021-01-11 19:28

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

Exception in thread \"main\" java.lang.IllegalArgumentException: can\'t serialize class mongodb.Person         


        
相关标签:
8条回答
  • 2021-01-11 20:08

    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.

    0 讨论(0)
  • 2021-01-11 20:11

    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

    0 讨论(0)
  • 2021-01-11 20:12

    You can achieve this by using following code:

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

    Now in your mongodb code, you can serialise a Person list as follows

    ....
    PersonList personList = new PersonList();
    personList.person = new ArrayList<>();
    // add persons to the list
    ....
    ....
    record.put("personsList", personList);
    ....
    // rest of your code
    
    0 讨论(0)
  • 2021-01-11 20:21

    class Person should implement java.io.Serializable interface.

    class Person implements Serializable

    0 讨论(0)
  • 2021-01-11 20:25

    Here is the code example to make Employee object serialized:

    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());
        }
    }
    
    0 讨论(0)
  • 2021-01-11 20:26

    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

    0 讨论(0)
提交回复
热议问题