spring-data-redis Jackson serialization

2020-07-27 03:00发布

问题:

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

回答1:

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.



回答2:

Have you tried disable serialization feature SerializationFeature.WRITE_DATES_AS_TIMESTAMPS?