gpt4 book ai didi

java - 父/子序列化本地类不兼容异常

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:55 28 4
gpt4 key购买 nike

我从以下两个类中收到序列化兼容性错误。只有父类CommericalCustomer 实现了序列化。当具有如下所示的父/子关系时,使用可序列化接口(interface)的正确方法是什么?

public class CachedCommercialCustomers extends CommercialCustomer {


}

public class CommercialCustomer implements Serializable {

private static final long serialVersionUID = 1L;

}

异常(exception):

[#|2013-01-02T05:01:02.553-0800|SEVERE|glassfish3.1.2|com.hazelcast.nio.AbstractSerializer|_ThreadID=10;_ThreadName=Thread-2;|spot.api.model.vo.backoffice.CachedCommercialCustomers; local class incompatible: stream classdesc serialVersionUID = -2672531984245897526, local class serialVersionUID = -743225273062282831
java.io.InvalidClassException: com.sample.CachedCommercialCustomers; local class incompatible: stream classdesc serialVersionUID = -2672531984245897526, local class serialVersionUID = -743225273062282831
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:579)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1600)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1513)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1749)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1346)
at java.io.ObjectInputStream.readUnshared(ObjectInputStream.java:458)
at com.hazelcast.nio.DefaultSerializer$ObjectSerializer.readNormal(DefaultSerializer.java:383)
at com.hazelcast.nio.DefaultSerializer$ObjectSerializer.read(DefaultSerializer.java:353)
at com.hazelcast.nio.DefaultSerializer.read(DefaultSerializer.java:134)
at com.hazelcast.nio.CustomSerializerAdapter.read(CustomSerializerAdapter.java:33)
at com.hazelcast.nio.AbstractSerializer.toObject(AbstractSerializer.java:116)
at com.hazelcast.nio.AbstractSerializer.toObject(AbstractSerializer.java:146)
at com.hazelcast.nio.Serializer.readObject(Serializer.java:72)
at com.hazelcast.impl.ThreadContext.toObject(ThreadContext.java:103)
at com.hazelcast.nio.IOUtil.toObject(IOUtil.java:149)
at com.hazelcast.impl.BaseManager$RequestBasedCall.getResultAsObject(BaseManager.java:384)
at com.hazelcast.impl.BaseManager$ResponseQueueCall.getResultAsObject(BaseManager.java:455)
at com.hazelcast.impl.BaseManager$RequestBasedCall.getResultAsObject(BaseManager.java:368)
at com.hazelcast.impl.BaseManager$ResponseQueueCall.getResultAsObject(BaseManager.java:455)

最佳答案

这里的问题是你没有为你的子类指定一个serialVersionId - 所以java会为你生成一个。如果更改 java 源文件,简单的重新编译可能会更改此值。请参阅spec .

当代码改变时 I 也会改变,这是一件好事,但在某些情况下,它会给你带来一些悲伤:)

参见here了解更多信息以及如何在必要时“解决”问题。

关于java - 父/子序列化本地类不兼容异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14129315/

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