Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-data-redis @Cacheable java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to MyObject

I am using spring-data-redis for caching data in my spring boot app. I am using Mongo as my primary data source and Redis as a cache. When I hit the API for the first time, it fetches record from Mongo and saves it in Cache, and returns MyObject correctly to the client. But when I hit the API second time, it finds the record in the Cache, and while trying to deserialize that back into MyObject, it . always runs into a cast exception:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to MyObject

Here is my Redis Configuration:

public class MyConfiguration {
    @Bean
    public CacheManager cacheManager(RedisTemplate<String, MyObject> redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }

    @Bean
    public RedisTemplate<String, MyObject> redisTemplate(RedisConnectionFactory connectionFactory, ObjectMapper objectMapper) {
        StringRedisSerializer serializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer hashValueSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);
        RedisTemplate<String, MyObject> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setValueSerializer(hashValueSerializer);
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

Found same problem reported here: Spring-data-redis @Cacheable java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

I researched for quite some time but have no ideas. Please suggest. Thanks a ton in advance.

like image 545
hardik.desai Avatar asked Feb 27 '18 19:02

hardik.desai


People also ask

What is ClassCastException in Java and how to fix it?

ClassCastException in Java occurs when we try to convert the data type of entry into another. This is related to the type conversion feature and data type conversion is successful only where a class extends a parent class and the child class is cast to its parent class.

Is Redis the data store for Spring Boot cache?

Overview In this short tutorial, we'll look at how to configure Redis as the data store for Spring Boot cache. Learn how we can enable multiple cache managers in our Spring Boot application.

What is ClassCastException in thread “main”?

Exception in thread “main” java.lang.ClassCastException: class java.math.BigDecimal cannot be cast to class java.lang.String (java.math.BigDecimal and java.lang.String are in module java.base of loader ‘bootstrap’) We can fix the exception printing by means of converting the code in the below format:


1 Answers

It worked for me by simply setting a new GenericJackson2JsonRedisSerializer() with no arguments, in both Key, value serializer and hash key, value serializer

@Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory, ObjectMapper objectMapper) {
        final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        // value serializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // hash value serializer
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        logger.info("wiring up Redistemplate...");
        return redisTemplate;
    }
like image 67
MvM FromMx Avatar answered Sep 21 '22 05:09

MvM FromMx