问题描述
我在我的一个Java应用程序中使用Redis,并且正在序列化要存储在Redis中的对象列表.但是,我注意到使用RedisTemplate将使用JdkSerializationRedisSerializer.相反,我想使用Jackson进行序列化,因为我认为这样做对速度更好.我该如何配置RedisTemplate改为使用Jackson?
I'm using redis in one of my java apps and I'm serializing a list of objects to be stored in Redis. However, I noticed that using the RedisTemplate would use the JdkSerializationRedisSerializer. Instead, I'd like to use Jackson to serialize since I believe it is better for speed. How would I go about configuring my RedisTemplate to use Jackson instead?
为澄清起见,这是我配置RedisTemplate的方式:
For clarification, this is how I'm configuring my RedisTemplate:
@Override
protected RedisConfiguration getRedisConfiguration() {
return redisConfiguration;
}
@Bean
public RedisTemplate<String, Object> getRedisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
推荐答案
像这样,
@Bean
public RedisTemplate<String, Object> getRedisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
Jackson2JsonRedisSerializer jrs = new Jackson2JsonRedisSerializer(String.class);
template.setKeySerializer(jrs);
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
,我建议您阅读此文档 https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:template
and, I suggest you read this documenthttps://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:template
这篇关于春季如何使用杰克逊代替JdkSerializationRedisSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!