我们知道系统中的RedisTemplate默认采用的是JDK的序列化机制,假如我们不希望使用默认的JDK方式序列化,可以对RedisTemplate对象进行定制,指定自己的序列化方式.
我们可以先参考底层默认的配置:如图
1.简单定制
新建一个配置类直接复制过来即可 再进行定制
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
本次将Key的序列化方式设置为String value的序列化方式设置为json,代码如下
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//设置key的序列化方式
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
//设置value的序列化方式
template.setValueSerializer(RedisSerializer.json());
template.setHashValueSerializer(RedisSerializer.json());
return template;
}
2高级定制
进一步定制json序列化:将RedisSerializer.json()替换为自己定义的方法jsonSerializer()
@Bean
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//设置key的序列化方式
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
//设置值的序列化方式
template.setValueSerializer(jsonSerializer());
template.setHashValueSerializer(jsonSerializer());
//更新一下RedisTemplate对象的默认配置
template.afterPropertiesSet();
return template;
}
1.定义Redis序列化,反序列化规范对象(此对象底层通过ObjectMapper完成对象序列化和反序列化)
public RedisSerializer jsonSerializer() {
Jackson2JsonRedisSerializer serializer =
new Jackson2JsonRedisSerializer(Object.class);
//创建ObjectMapper(有jackson api库提供)对象,基于此对象进行序列化和反序列化
//创建ObjectMapper对象
ObjectMapper objectMapper=new ObjectMapper()
return null;
}
2.设置按哪些方法规则进行序列化,对象属性值为null时,不进行序列化存储
public RedisSerializer jsonSerializer() {
Jackson2JsonRedisSerializer serializer =
new Jackson2JsonRedisSerializer(Object.class);
//创建ObjectMapper(有jackson api库提供)对象,基于此对象进行序列化和反序列化
//创建ObjectMapper对象
ObjectMapper objectMapper=new ObjectMapper()
//设置按哪些方法规则进行序列化
objectMapper.setVisibility(PropertyAccessor.GETTER,//get方法
JsonAutoDetect.Visibility.ANY);//Any 表示任意方法访问修饰符
//对象属性值为null时,不进行序列化存储
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return null;
}
激活序列化类型存储.即可全部定制完毕
public RedisSerializer jsonSerializer() {
Jackson2JsonRedisSerializer serializer =
new Jackson2JsonRedisSerializer(Object.class);
//创建ObjectMapper(有jackson api库提供)对象,基于此对象进行序列化和反序列化
//创建ObjectMapper对象
ObjectMapper objectMapper=new ObjectMapper()
//设置按哪些方法规则进行序列化
objectMapper.setVisibility(PropertyAccessor.GETTER,//get方法
JsonAutoDetect.Visibility.ANY);//Any 表示任意方法访问修饰符
//对象属性值为null时,不进行序列化存储
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//假如没有这个配置,redis存储数据时不存储类型,反序列化时会默认将其数据存储到map
objectMapper.activateDefaultTyping(
objectMapper.getPolymorphicTypeValidator(),//多态校验分析
ObjectMapper.DefaultTyping.NON_FINAL,//激活序列化类型存储,类不能使用final修饰
JsonTypeInfo.As.PROPERTY);//PROPERTY 表示类型会以json对象属性形式存储
serializer.setObjectMapper(objectMapper);
return serializer;
}