---
title: "How to configure custom Redis serialization in Spring Boot?"  
description: "How to configure custom Redis serialization in Spring Boot?"  
author: "Lily Chitlangiya"  
published: 2026-09-17  
updated: 2026-10-12  
canonical: https://answers.mindstick.com/qa/117201/how-to-configure-custom-redis-serialization-in-spring-boot  
category: "Spring Boot"  
tags: ["Spring Boot", "java", "Redis", "Serialization"]  
reading_time: 2 minutes  

---

# How to configure custom Redis serialization in Spring Boot?

By default, **Spring Data Redis** uses standard Java JDK serialization, which outputs unreadable binary strings into Redis keys. Replacing this default setup with **[GenericJackson2JsonRedisSerializer](https://www.mindstick.com/forum/34569/json)** 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.

```java
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;
    }
}
```

## Answers

### Answer by Amelia D'cruz

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:

```java
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.


---

Original Source: https://answers.mindstick.com/qa/117201/how-to-configure-custom-redis-serialization-in-spring-boot

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
