Po类valid字段原本是Boolean类型,数据库字段为tinyint,将valid字段类型改为Integer后redis中缓存的老数据无法序列化
//老实体
@Data
public class CtReqParam implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private boolean valid;
private String name;
}
@Data
public class CtReqParam implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
//变更字段类型
private Integer valid;
private String name;
}
增加配置类,采用fastjson序列化可解决该问题
@Configuration
public class RedisConfig {
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration=configuration.serializeValuesWith(RedisSerializationContext
.SerializationPair
.fromSerializer(fastJsonRedisSerializer))
.entryTtl(Duration.ofDays(1)
);
return configuration;
}
private static class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
static {
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
public FastJsonRedisSerializer(Class<T> clazz) {
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
}
缓存对象在redis客户端可查看