序列化/反序列化 ClassCastException:x 无法转换为 java.io.ObjectStreamClass

2024-02-09

使用 Java 本机序列化,我间歇性地看到 ClassCastException

java.lang.ClassCastException: myCompany.MyClass$MembershipServiceMethod cannot be cast to java.io.ObjectStreamClass

或(不太频繁)

java.lang.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass

当我反序列化特定不可变类的对象时。也就是说,对于特定的序列化表示总是会抛出异常,但大多数对象都可以成功序列化和反序列化。

public final class ServiceInteractionImpl implements ServiceInteraction, Serializable {
private static final long serialVersionUID = 1L;

private final InteractionSource source;
private final long startTime;
private final InteractionType type;
private final ServiceMethod method;
private final long duration;
private final String accompanyingMessage;

public ServiceInteractionImpl(final ServiceMethod method,
                                  final InteractionSource source,
                                  final InteractionType type,
                                  final long startTime,
                                  final long duration,
                                  final String accompanyingMessage) {
            this.source = source;
            this.startTime = startTime;
            this.duration = duration;
            this.type = type;
            this.method = method;
            this.accompanyingMessage = accompanyingMessage;
    }

    ...

    getters and canonical methods
}

其中 InteractionSource、InteractionType 和 ServiceMethod 是枚举。 我无法在运行序列化和反序列化数百万个对象的 junit 测试的本地环境中复制此内容。

这是编写代码

    fileLock.lock();
    try {
        final File recordFile = new File(recordFileName);
        boolean appendToFile = false;

        if (recordFile.exists()) {
            appendToFile = true;
        }

        final OutputStream fileOutputStream = new FileOutputStream(recordFileName, true);
        final OutputStream buffer = new BufferedOutputStream(fileOutputStream);

        ObjectOutput output;
        if (appendToFile) {
            output = new AppendableObjectOutputStream(buffer);
        } else {
            output = new ObjectOutputStream(buffer);
        }

        int localSerializedInteractionsCount = 0;

        try {
            for (final ServiceInteraction interaction : tempCollection) {
                output.writeObject(interaction);
                output.flush();
                localSerializedInteractionsCount++;
                rollBackCollection.remove(interaction);
            }
        } finally {
            output.close();
            serializedInteractionCount += localSerializedInteractionsCount;
        }
    } catch (IOException e) {
        LOGGER.error("could not write to file", e);
        //some roll-back code
    } finally {
        fileLock.unlock();
    }

see 附加到 ObjectOutputStream https://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream对于可附加对象输出流

非常感谢任何帮助。


如果类定义发生更改而未更新serialVersionUID,则这些错误可能来自基于旧类定义的陈旧对象实例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

序列化/反序列化 ClassCastException:x 无法转换为 java.io.ObjectStreamClass 的相关文章

随机推荐