java - 移动类包时抛出 Serializable class not found CLASSNAME 错误(即使 serialVersionUID 已在类中说明)

标签 java redis serializable redisson serialversionuid

我正在使用 Redis(通过 redisson)我有一个 DTO 类,我正在将其序列化并反序列化到 Redis,

我使用的编解码器是:

org.redisson.codec.FstCodec

尽管在类的 DTO 类中显式设置了 serialVersionUID,但当我将该类移动到不同的命名空间时,我得到以下异常:

java.io.IOException: java.lang.RuntimeException: class not found CLASSNAME:db.data.coins.CoinDTO loader:jdk.internal.loader.ClassLoaders$AppClassLoader@6ed3ef1 

在示例中,我将类从包中移出:

db.data.coins.CoinDTO

打包:

dto

fstCodec github链接:

link

package dto; // altering package

import java.io.Serializable;
import java.sql.Timestamp;

public class CoinDTO implements Serializable {

    static final long serialVersionUID = 1L;

    private int id;

    private double amount;

    private Timestamp timestamp;

   //Getters and setters

}

我想更改包并仍然从 Redis 获取类。

提前致谢

附言

我目前处理它的方式是使用 json 序列化对象,然后将其设置到 redis 中,但这只是双重序列化,我想避免这种情况

最佳答案

when I move the class to a diffrent namespace despite setting the: serialVersionUID explicitly in the DTO class in class I get the following exception:

如果对象的包名或类名已更改,则无法反序列化对象。

The way I am currently handling it is by serializing of the object using json and then setting it into redis

您可以将 Redisson 设置为 Jackson,如下所示 codec = org.redisson.codec.JsonJacksonCodec

关于java - 移动类包时抛出 Serializable class not found CLASSNAME 错误(即使 serialVersionUID 已在类中说明),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54266920/

相关文章:

java - 存储结果集供以后获取

java - 我的代码有什么问题数据源拒绝建立连接,来自服务器的消息 : "Too many connections"

spring-boot - Spring Data Redis 覆盖默认序列化器

Java Swing JTable 数据收集在最后一个单元格上失败

java - 在没有 GUI 的情况下运行 Netbeans 平台应用程序

python - 如何从 python 和 rust 存储和读取 redis 中的公共(public)数据?

mysql - NoSQL 快速简单的海量数据存储

java - 在 Serialized 类的构造函数中定义对象

java - 有没有办法让当前类指向Java Serialized中刚刚读取的新对象?

java - NotSerializedException GroupLayout 通过 RMI 发送 Frame