'reactive redis serialization issue. Cannot serialize value of type class java.lang.Integer without a serializer error generated when saving in redis

I am trying to store an object in redis and I am getting this error. reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalStateException: Cannot serialize value of type class java.lang.Integer without a serializer I have included serializers for both value and hashvalue but it doesnt work. below is my code

config

    public ReactiveRedisOperations<String, Player> playerRedisOperations(LettuceConnectionFactory connectionFactory) {
        RedisSerializationContext<String, Player> serializationContext = RedisSerializationContext
                .<String, Player>newSerializationContext(new StringRedisSerializer())
                .key(new StringRedisSerializer())
                .value(new GenericToStringSerializer<>(Player.class))
                .hashKey(new StringRedisSerializer())
                .hashValue(new GenericJackson2JsonRedisSerializer())
                .build();
        return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
    }

redis repository

    private final ReactiveRedisOperations<String, Player> redisOperations;
    private final ReactiveHashOperations<String, Integer, Player> hashOperations;
    
    public PlayerRedisRepositoryImpl(@Qualifier("playerRedisOperations")ReactiveRedisOperations<String, Player> redisOperations) {
        this.redisOperations = redisOperations;
        this.hashOperations = redisOperations.opsForHash();
    }
    
    public Mono<Player> savePlayer(String key,Player player) {
        return hashOperations.put(key, player.user_id(), player).map(isSaved -> player);
    }

stacktrace

reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalStateException: Cannot serialize value of type class java.lang.Integer without a serializer
Caused by: java.lang.IllegalStateException: Cannot serialize value of type class java.lang.Integer without a serializer
    at org.springframework.data.redis.serializer.DefaultRedisElementWriter.write(DefaultRedisElementWriter.java:56)
    at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.write(RedisSerializationContext.java:287)
    at org.springframework.data.redis.core.DefaultReactiveHashOperations.rawHashKey(DefaultReactiveHashOperations.java:339)
    at org.springframework.data.redis.core.DefaultReactiveHashOperations.lambda$put$20(DefaultReactiveHashOperations.java:251)
    at org.springframework.data.redis.core.DefaultReactiveHashOperations.lambda$createMono$27(DefaultReactiveHashOperations.java:324)
    at org.springframework.data.redis.core.ReactiveRedisTemplate.lambda$doInConnection$1(ReactiveRedisTemplate.java:244)
    at reactor.core.publisher.FluxUsingWhen.deriveFluxFromResource(FluxUsingWhen.java:119)

what could be the issue?



Solution 1:[1]

The root cause is in your config. The value of hashKey is expected for StringRedisSerializer() but you are passing it an Integer. Replace .hashKey(new StringRedisSerializer()) to .hashKey(new Jackson2JsonRedisSerializer<>(Integer.class))

The correct configuration is:

public ReactiveRedisOperations<String, Player> playerRedisOperations(LettuceConnectionFactory connectionFactory) {
    RedisSerializationContext<String, Player> serializationContext = RedisSerializationContext
            .<String, Player>newSerializationContext(new StringRedisSerializer())
            .key(new StringRedisSerializer())
            .value(new GenericToStringSerializer<>(Player.class))
            .hashKey(new Jackson2JsonRedisSerializer<>(Integer.class))
            .hashValue(new GenericJackson2JsonRedisSerializer())
            .build();
    return new ReactiveRedisTemplate<>(connectionFactory, serializationContext);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anicetkeric