我正在使用 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链接:
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/