gpt4 book ai didi

java - 反序列化时出现异常

转载 作者:行者123 更新时间:2023-12-02 11:38:50 24 4
gpt4 key购买 nike

我正在尝试反序列化一个实现可序列化接口(interface)的类并扩展不可序列化的类。

/**
*
*/
package com.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class SerializationTest implements Serializable {

/**
*
*/
private static final long serialVersionUID = -1324438308227634614L;

class Papa{
Papa(){
System.out.println("Papa called..");
}
}
class Student extends Papa implements Serializable{

/**
*
*/
private static final long serialVersionUID = 8667392485783922740L;

String name;
int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

/**
*
*/
public Student() {
System.out.println("Constructor called");
}

}

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Student arvind = new SerializationTest().new Student();
arvind.setName("Arvind");
arvind.setId(123);

serialize(arvind);
System.out.println("Serialization done..");
deserialize();
}

/**
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*
*/
private static void deserialize() throws FileNotFoundException, IOException, ClassNotFoundException {

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("tests.txt"));

Student arvind = (Student)ois.readObject();
System.out.println("Deserialize name - " + arvind.getName());
System.out.println(arvind.getId());
ois.close();

}

/**
* @param arvind
* @throws IOException
* @throws FileNotFoundException
*/
private static void serialize(Student arvind) throws IOException, FileNotFoundException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tests.txt"));

oos.writeObject(arvind);
oos.flush();
oos.close();
}
}

我收到以下异常 -

Exception in thread "main" java.io.InvalidClassException: com.test.SerializationTest$Student; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.test.SerializationTest.desseialize(SerializationTest.java:88)
at com.test.SerializationTest.main(SerializationTest.java:75)

我已经关注了java.io.InvalidClassException: no valid constructor的回答通过创建默认构造函数,但它也没有帮助。

非常感谢任何建议和帮助。

最佳答案

您所需要的只是将 Student 和 Papa 公共(public)类放在单独的文件中,或者像这样将它们设置为静态。

static class Papa{
Papa(){
System.out.println("Papa called..");
}
}
static class Student extends Papa implements Serializable{

/**
*
*/
private static final long serialVersionUID = 8667392485783922740L;

String name;
int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

/**
*
*/
public Student() {
System.out.println("Constructor called");
}

}

您的代码不起作用的原因是内部 Papa 类的构造函数无法在 Student 类的构造函数中直接调用。我们将 Papa 设为静态以摆脱这种束缚。

关于java - 反序列化时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48725308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com