本文介绍了spring-data-redis Jackson序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用spring-data-redis的Jackson序列化功能。我正在构建一个ObjectMapper并使用GenericJackson2JsonRedisSerializer作为redisTemplate的序列化器:

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;
        }
    }

我有一个SampleBean我正在尝试保存:

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;
        }
    }

以及该bean的存储库:

And a repository for that bean:


    public interface SampleBeanRepository extends CrudRepository {
    }

然后我尝试将bean写入Redis:

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();

我希望redisTemplate使用Serializer在SampleBean中写入Date作为一个时间戳,但它写成一个long。

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.

相关的spring-data-redis引用:
完整代码示例:

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

推荐答案

模板使用的序列化程序/映射器不会影响存储库使用的序列化程序/映射程序,因为存储库直接使用转换器 byte [] 进行操作基于域类型元数据读/写数据的实现。

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.

这篇关于spring-data-redis Jackson序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:09