在迁移项目中, 遇到了map结构存储的json文本不一致问题
A项目使用的是jedis
B项目使用的是redisson
因此B项目需要使用自定义的编码器, json序列化框架是springBoot自带的jackson
public <T> RMap<String, T> getRMap(RedisKey redisKey, Class<T> clazz, String key) {
RMap<String, T> mapCache = redissonClient.getMap(key, new MyCodec(clazz));
return mapCache;
}
public class MyCodec extends BaseCodec {
private TypedJsonJacksonCodec typedJsonJacksonCodec;
private StringCodec stringCodec;
public MyCodec(Class<?> valueClass) {
typedJsonJacksonCodec = new TypedJsonJacksonCodec(valueClass);
stringCodec = new StringCodec();
}
@Override
public Decoder<Object> getValueDecoder() {
return stringCodec.getValueDecoder();
}
@Override
public Encoder getValueEncoder() {
return stringCodec.getValueEncoder();
}
@Override
public Decoder<Object> getMapValueDecoder() {
return typedJsonJacksonCodec.getValueDecoder();
}
@Override
public Encoder getMapValueEncoder() {
return typedJsonJacksonCodec.getValueEncoder();
}
@Override
public Decoder<Object> getMapKeyDecoder() {
return stringCodec.getValueDecoder();
}
@Override
public Encoder getMapKeyEncoder() {
return stringCodec.getValueEncoder();
}
}