org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type to type [java.lang.String] - Redis

ぃ、小莉子 提交于 2019-12-06 05:22:42

You should use a serializer before persisting your object. See this part of the Spring Data Redis docs:

Data can be stored by using various data structures within Redis. Jackson2JsonRedisSerializer can convert objects in JSON format. Ideally, JSON can be stored as a value by using plain keys. You can achieve a more sophisticated mapping of structured objects by using Redis hashes. Spring Data Redis offers various strategies for mapping data to hashes (depending on the use case):

  • Direct mapping, by using HashOperations and a serializer
  • Using Redis Repositories
  • Using HashMapper and HashOperations

EDIT:

Replace your GenericToStringSerializer in the RedisTemplate bean definition with JdkSerializationRedisSerializer (the default serializer) or Jackson2JsonRedisSerializer or GenericJackson2JsonRedisSerializer to serialize in JSON format.

As per guidance from @Gustavo Passini and taking reference from https://www.programcreek.com/java-api-examples/index.php?api=org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.

Here is the

@Bean
    public RedisTemplate<String, Object> redisTemplate(){
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        final RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!