Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates?

Related searches

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

I have several DAOs which handle different Model classes:

For example, ShopperHistoryDao save/retrieve objects of ShopperHistoryModel, ShopperItemHistoryDao save/retrieve objects of ItemHistoryModel.

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

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

JacksonJsonRedisSerializer(Class<T> type)

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

Something like:

<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 should do the job

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

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\"}"

In the service you can cache the model using

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

Check this article: http://blog.pranavek.com/2016/12/25/integrating-redis-with-spring-application

Spring RedisTemplate:将多个Model类序列化为JSON。需要使用多 , [英]Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates? 本文翻译自 Ashika Umanga� 19 Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates? [Fatal Error] : Class 'Model_Winners' not found Jun 23 '14.

Yes, the RedisTemplate seems to be designed to have a single instance of the value serializer.

I was going to suggest the possible workaround of having a RedisSerializer which contains a Map of inner serializers so you can use one RedisTemplate with a serializer that can handle multiple types - but since RedisSerializer does not offer methods like boolean canDeserialize(..) (as the HTTP MessageConverters in Spring MVC have) this doesn't seem possible.

So it seems that you are stuck with having multiple RedisTemplate instances.

Spring RedisTemplate : use same key with multiple RedisTemplate , For e.g. I have redisTemplate1 and redisTemplate2 beans defined in spring, like. <bean id="redisTemplate1" class ="org.springframework.data.redis.core. Student So, my questions are, Is it possible to use multiple redis templates ? // This is how you put your data in Redis //Redisson serialize the data into JSON format� 21 Spring + Hibernate : a different object with the same identifier value was already associated with the session 21 Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates?

A bit of old thread, but you can do something like this:

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

Then in your Java class

@Autowire
private RedisTemplate redisTemplate;

public void save(Model model) {
    ObjectMapper obmap = new ObjectMapper();
    redisTemplate.opsForHash().putAll(mode.getId(), obmap.convertValue(model, Map.class));
}

关于java:Spring RedisTemplate:将多个Model类序列化为JSON , Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates?我正在使用Spring Redis支持将我的� 21 Spring + Hibernate : a different object with the same identifier value was already associated with the session 21 Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates?

Spring Boot Redis custom json serializer example. � GitHub, I have several DAOs which handle different Model classes : eg : ' ShopperHistoryDao' which will save/retrieve objects of Need to use Multiple RedisTemplates? 由匿名(未验证) I am using Spring Redis support to save my objects in Redis. Spring RedisTemplate : 여러 모델 클래스를 JSON으로 serialize합니다. 여러 RedisTemplates를 사용해야합니까? Redis에서 내 개체를 저장하기 위해 Spring Redis 지원을 사용하고 있습니다.

Spring RedisTemplate : Serialise multiple Model classes into JSON , The Spring Data Redis project applies core Spring concepts to the development of solutions by using a key-value style data store. We provide a “template” as a high-level abstraction for sending and receiving messages.

Solve The Problem. February 22, 2020 Jquery Leave a comment. Questions: I am very new to jQuery. The first part of the code is to check the inputs and after checked it should display the checked items the below format.

Comments
  • old, but maybe one can extend redisJson Serializer and make the use of fasterxml.jackson in order to make it more generic. i.e not forcing it to be bound to a single class definition?
  • what about public List<WhitePaper> findWhitePapersByTitle(String title) ??
  • Can you share the source code ? Am getting the following error "Cannot handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type "