3

I'm attempting to use the Jackson serialization feature of spring-data-redis. I am building a ObjectMapper and using the GenericJackson2JsonRedisSerializer as the serializer for the redisTemplate:



    @Configuration
    public class SampleModule {
        @Bean
        public ObjectMapper objectMapper() {
            return Jackson2ObjectMapperBuilder.json()
                    .serializationInclusion(JsonInclude.Include.NON_NULL) // Don’t include null values
                    .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) //ISODate
                    .build();
        }

        @Bean
        public RedisTemplate getRedisTemplate(ObjectMapper objectMapper, RedisConnectionFactory redisConnectionFactory){
            RedisTemplate redisTemplate = new RedisTemplate();
            redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            return redisTemplate;
        }
    }

I have a SampleBean I am attempting to save:



    @RedisHash("sampleBean")
    public class SampleBean {
        @Id
        String id;
        String value;
        Date date;

        public SampleBean(String value, Date date) {
            this.value = value;
            this.date = date;
        }
    } 

And a repository for that bean:



    public interface SampleBeanRepository extends CrudRepository {
    }

I am then trying to write the bean to Redis:



    ConfigurableApplicationContext context =    SpringApplication.run(SampleRedisApplication.class, args);

    SampleBean helloSampleBean = new SampleBean("hello", new Date());
    ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
    logger.info("Expecting date to be written as: " + objectMapper.writeValueAsString(helloSampleBean.date));

    SampleBeanRepository repository = context.getBean(SampleBeanRepository.class);
    repository.save(helloSampleBean);

    context.close();

I expect the redisTemplate to use the Serializer to write the Date inside of the SampleBean as a Timestamp, however it is written as a long.

The relevant spring-data-redis reference: http://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:serializer Full code sample: https://github.com/bandyguy/spring-redis-jackson-sample-broken

dev_b_459821
  • 41
  • 1
  • 1
  • 3

2 Answers2

4

The serializer/mapper used by the template does not affect the one used by the repository since the repository directly operates upon the byte[] using Converter implementations for reading/writing data based on domain type metadata.

Please refer to the Object to Hash Mapping section of the reference manual for guidance how to write and register a custom Converter.

Christoph Strobl
  • 6,491
  • 25
  • 33
  • With redis repositories, is it possible to add a generic converter which is used for all types? For example, we can set GenericJackson2JsonRedisSerializer as the hash value serializer on the redis template, but as you pointed out, it will not be used by repository. So is there a way of setting a default hash value serializer which will be used by the repository? – Rahul Sharma May 13 '20 at 05:02
  • Without any custom serializer, if I have a nested structure such as Map where object is a map of maps, I get stack overflow errors. – Rahul Sharma May 13 '20 at 05:42
0

Have you tried disable serialization feature SerializationFeature.WRITE_DATES_AS_TIMESTAMPS?

WesternGun
  • 11,303
  • 6
  • 88
  • 157