我正在尝试使用 RSet 来存储我的排名,但是当类存在时我遇到了 ClassNotFoundException(我什至反编译了并且类在这里)。在我看来,Redisson 无法存储自定义对象类? 这是我的代码:
public class RedisProvider {
private final RSet<Rank> ranks;
private final RedissonClient client;
public RedisProvider() {
this.client = Redisson.create();
this.ranks = client.getSet("ranks");
Rank rank = new Rank("default", 0, "&7", "&7");
addRank(rank);
}
public Set<Rank> getRanks(){
return ranks.readAll();
}
public void addRank(Rank rank) {
ranks.add(rank);
}
}
错误:
[17:51:39 ERROR]: Unable to decode data. channel: [id: 0xb12f3447,
L:/127.0.0.1:58768 - R:127.0.0.1/127.0.0.1:6379], reply:
ReplayingDecoderByteBuf(ridx=146, widx=146), command: (SSCAN), promise:
java.util.concurrent.CompletableFuture@4e7fd89e[Not completed, 1
dependents], params: [ranks, 0, COUNT, 10] java.io.IOException:
java.lang.ClassNotFoundException: fr.rhodless.test.Rank
我检查了“Rank”类是否存在并且它存在。
您可以以“ByteArrayCodec”/“JsonJacksonCodec”的形式编写自己的“org.redisson.client.codec.Codec”实现 例如,我根据我们的需求写了一个。
import org.redisson.client.codec.BaseCodec;
import org.redisson.client.protocol.Decoder;
import org.redisson.client.protocol.Encoder;
import io.netty.buffer.Unpooled;
/**
* Encoder/Decoder for {@code Codec}.
*/
final class SnappyCodec extends BaseCodec {
static final SnappyCodec INSTANCE = new SnappyCodec();
private static final Encoder ENCODER = in -> Unpooled.wrappedBuffer(SnappyCompressor.getInstance().serializeAndCompress(in));
private static final Decoder<Object> DECODER = (buf, state) -> {
final byte[] result = new byte[buf.readableBytes()];
buf.readBytes(result);
return SnappyCompressor.getInstance().uncompressAndDeserializeWithFallbackToDeserializedDataBuffer(result);
};
@Override
public Decoder<Object> getValueDecoder() {
return DECODER;
}
@Override
public Encoder getValueEncoder() {
return ENCODER;
}
}