DEV Community

srinathgithub
srinathgithub

Posted on

Spring-data-redis : Facing Null pointer Exception

Unable to understand why I'm getting null when trying to access data from Map. data present in Redis and same flow to a java application, when I print the map I can see data in the map but on access through null

Code is below
Service Layer
@Cacheable(value = "DISCOUNT_COMPONENTS", unless = "#result == null")
@Override
public Map getDiscountComponents(
String discountComponentsKey) {
List discountComponentsEntity = discountComponentRepository
.findAll();
Map discountComponentsMap = new HashMap();
if (!CollectionUtils.isEmpty(discountComponentsEntity)) {
for (final DiscountComponent discountComponentEntity : discountComponentsEntity) {
discountComponentsMap.put(
discountComponentEntity.getDiscountComponentId(),
discountComponentEntity.getDiscountComponentName());
}
}
return discountComponentsMap;
}

Calling from Controller layer

 @RequestMapping(value = "/from-cache-map_2/{key}/{key2}", method = RequestMethod.GET)
    public void test(@PathVariable Integer key, @PathVariable Integer key2) {
        Map<Integer, String> discountComponentMap =  discountCacheService
                .getDiscountComponents(PricingBusinessConstants.DISCOUNT_COMPONENTS_KEY);
        LOGGER.info("**************discountComponentMap **********" + discountComponentMap);
        // Getting cached categories information
        LOGGER.info("Data in Map : "+discountComponentMap.get(key));
        LOGGER.info("Key Contains check "+discountComponentMap.containsKey(key));
        LOGGER.info("Key Contains check "+discountComponentMap.containsKey(1));

}

Logs
**************discountComponentMap **********{1=CLASS, 2=TEMPLATE, 3=CATEGORY}
Data in Map : null
Key Contains check false
Key Contains check false
Enter fullscreen mode Exit fullscreen mode

Redis Configuration :

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisSerializer<Object> serializer = redisSerializer();
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    template.setKeySerializer(serializer);
    template.setValueSerializer(serializer);
    template.setHashKeySerializer(serializer);
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();
    return template;
}

@Bean
public RedisSerializer<Object> redisSerializer() {
    //Create JSON serializer
    Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    serializer.setObjectMapper(objectMapper);
    return serializer;
}

@Bean
@Primary
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {

   /* RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
            .disableCachingNullValues()
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
    redisCacheConfiguration.usePrefix();*/

    /*RedisCacheManager cacheManager = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
            .build();*/
    //cacheManager.setTransactionAware(true);

    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate(redisConnectionFactory));
    redisCacheManager.setTransactionAware(true);
    redisCacheManager.setCachePrefix(new DefaultRedisCachePrefix());
    redisCacheManager.setUsePrefix(true);
    redisCacheManager.setCachePrefix(new DefaultRedisCachePrefix("::"));
    redisCacheManager.setExpires(setKeyToTimeToLiveMap());
    return redisCacheManager;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)