Search in sources :

Example 1 with StringRedisTemplate

use of org.springframework.data.redis.core.StringRedisTemplate in project springboot_op by SnailFastGo.

the class RedisConfiguration method redisTemplate.

/**
     * RedisTemplate配置
     * @param factory
     * @return
     */
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    //定义value的序列化方式
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
Also used : Jackson2JsonRedisSerializer(org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) Bean(org.springframework.context.annotation.Bean)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Bean (org.springframework.context.annotation.Bean)1 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)1 Jackson2JsonRedisSerializer (org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer)1