Spring RedisTemplate:将多个Model类序列化为JSON。需要使用多个RedisTemplates吗? [英] Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates?

查看:113
本文介绍了Spring RedisTemplate:将多个Model类序列化为JSON。需要使用多个RedisTemplates吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Redis支持在Redis中保存我的对象。

I am using Spring Redis support to save my objects in Redis.

我有几个处理不同Model类的DAO:

I have several DAOs which handle different Model classes :

例如:'ShopperHistoryDao'将保存/检索'ShopperHistoryModel'
'ShopperItemHistoryDao'的对象,它将处理'ItemHistoryModel'的对象

eg : 'ShopperHistoryDao' which will save/retrieve objects of 'ShopperHistoryModel' 'ShopperItemHistoryDao' which will handle objects of 'ItemHistoryModel'

我想使用'JacksonJsonRedisSerializer'将对象序列化/反序列化为json。

I want to use 'JacksonJsonRedisSerializer' to serialise/deserialize my objects to/from json.

但是在JacksonJsonRedisSerializer的构造函数中,它需要一个特定的Model类。

But in the constructor of JacksonJsonRedisSerializer, it takes one specific Model class.

JacksonJsonRedisSerializer(Class<T> type)

这是否意味着,我必须为每个不同的Model类配置单独的RedisTemplates并在适当的DAO实现中使用它们?

Does that mean, I have to configure separate RedisTemplates for each different Model class and use them in appropriate DAO implementation?

类似于:

<bean id="redisTemplateForShopperHistoryModel" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory" />
    <property name="valueSerializer">
        <bean id="redisJsonSerializer" 
                        class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer">
            <constructor-arg type="java.lang.Class" value="ShopperHistoryModel.class"/>
        </bean>   
    </property>
</bean>


<bean id="redisTemplateForItemHistoryModel" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory" />
    <property name="valueSerializer">
        <bean id="redisJsonSerializer" 
                        class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer">
            <constructor-arg type="java.lang.Class" value="ItemHistoryModel.class"/>
        </bean>   
    </property>
</bean>


推荐答案

GenericJackson2JsonRedisSerializer 应该完成这项工作

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());                                           
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

这会将 @Class 属性添加到JSON了解类型,这有助于Jackson反序列化,因此无需在配置类上显式映射模型。

This will add @Class property to the JSON to understand the type, which helps Jackson to deserialize, so no need to explicitly map the model on the configuration class.

"{\"@class\":\"com.prnv.model.WhitePaper\",\"title\":\"Hey\",\"author\":{\"@class\":\"com.prnv.model.Author\",\"name\":\"Hello\"},\"description\":\"Description\"}"

在服务中,您可以使用

    @Cacheable(value = "whitePaper", key = "#title")
    public WhitePaper findWhitePaperByTitle(String title) 
    {
        WhitePaper whitePaper = repository.findByTitle(title);
        return whitePaper;
    }

查看这篇文章: http://blog.pranavek.com/2016/12/25/integrating-redis-with-spring-申请

这篇关于Spring RedisTemplate:将多个Model类序列化为JSON。需要使用多个RedisTemplates吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆