【问题标题】:spring-data-redis Jackson serializationspring-data-redis Jackson 序列化
【发布时间】:2017-07-21 10:18:05
【问题描述】:

我正在尝试使用 spring-data-redis 的 Jackson 序列化功能。我正在构建一个 ObjectMapper 并使用 GenericJackson2JsonRedisSerializer 作为 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:



    @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 的存储库:



    public interface SampleBeanRepository extends CrudRepository {
    }

然后我尝试将 bean 写入 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 写为 Timestamp,但是它写为 long。

相关spring-data-redis参考:http://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:serializer 完整代码示例:https://github.com/bandyguy/spring-redis-jackson-sample-broken

【问题讨论】:

    标签: spring jackson spring-data spring-data-redis


    【解决方案1】:

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

    有关如何编写和注册自定义Converter 的指导,请参阅参考手册的Object to Hash Mapping 部分。

    【讨论】:

    • 使用 redis 存储库,是否可以添加用于所有类型的通用转换器?例如,我们可以将 GenericJackson2JsonRedisSerializer 设置为 redis 模板上的哈希值序列化器,但正如您所指出的,存储库不会使用它。那么有没有办法设置存储库将使用的默认哈希值序列化程序?
    • 如果没有任何自定义序列化程序,如果我有一个嵌套结构,例如 Map,其中 object 是映射的映射,我会收到堆栈溢出错误。
    【解决方案2】:

    您是否尝试过禁用序列化功能SerializationFeature.WRITE_DATES_AS_TIMESTAMPS

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 2018-09-23
      • 2018-04-17
      • 2013-02-25
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多