How to configure custom Redis serialization in Spring Boot?

Asked 1 month ago Updated 15 days ago 190 views

0

By default, Spring Data Redis uses standard Java JDK serialization, which outputs unreadable binary strings into Redis keys. Replacing this default setup with GenericJackson2JsonRedisSerializer enables readable JSON representations and seamless deserialization.

Spring Boot Redis Configuration

You can override default serialization by registering a custom RedisTemplate bean inside a configuration class.

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate<>();
        // Connect template to underlying Redis client factory
        template.setConnectionFactory(factory);
        
        // Set plain string serializer for key names
        template.setKeySerializer(new StringRedisSerializer());
        
        // Set JSON serializer for stored values
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        
        return template;
    }
}

1 Answer


0

In Spring Boot, configure serialization on a RedisTemplate bean. Boot can still create the RedisConnectionFactory from your Redis connection settings; defining your own template lets you choose how keys and values are stored.

This example keeps keys readable while storing values as JSON:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(
            RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        // Keep Redis keys and hash field names readable.
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);

        // Store values and hash values as JSON.
        GenericJackson2JsonRedisSerializer jsonSerializer =
                new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(jsonSerializer);
        template.setHashValueSerializer(jsonSerializer);

        return template;
    }
}

With this setup, values read back through the generic serializer are not necessarily instances of the exact class you originally saved. The serializer includes type information to support object reconstruction, so use it only with data you trust. If your application stores one known model type, prefer a serializer configured for that type and use a correspondingly typed template.

For plain strings, use Boot’s StringRedisTemplate instead; it already uses string serialization. Also, these settings apply to operations through this RedisTemplate bean. If you use Spring Cache, configure the serializers on its RedisCacheManager separately.

Write Your Answer