1

I need to add a Redis cache in a method that returns a list of values.

I'm using this tutorial as basis https://www.baeldung.com/spring-data-redis-tutorial

The exception shows this

java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap')
    at 

@Cacheable(cacheNames = "customerDetailByParam", key="{#searchParams.toString()}")
    @Retryable(value = { HttpServerErrorException.class }, maxAttempts =  RETRY_ATTEMPTS, backoff = @Backoff(delay = 5000))
    public List<ObjectResponse> searchCustomerDetailByParam(MultiValueMap<String, String> searchParams) 

I've been looking for some solutions, however, none seems to work.

CacheConfig.java

@Configuration
@EnableCaching
@ConditionalOnMissingBean(value = CacheManager.class)
@Slf4j
public class CacheConfig {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
        //redisStandaloneConfiguration.setPassword(RedisPassword.of("yourRedisPasswordIfAny"));
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(getClass().getClassLoader());

        template.setDefaultSerializer(serializer);

        return template;
    }


}

ObjectResponse.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ObjectResponse implements Serializable {

    @JsonProperty("id")
    private String customerId;

    @JsonProperty("name")
    @JsonAlias("full_name")
    private String customerName;

    private String document;

    private String email;

}

1 Answer 1

2

I was able to fix the problem changing the template to the following configuration.

  @Bean
  public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.