Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jackson instead of JdkSerializationRedisSerializer in spring

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?

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;
}
like image 967
DanielD Avatar asked Nov 01 '25 12:11

DanielD


1 Answers

like this,

@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;
}

and, I suggest you read this document https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:template

like image 184
dai Avatar answered Nov 03 '25 02:11

dai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!