Commit a0b698bb by hulei

统一jacksonRedisTemplate

parent 92168b11
package com.brilliance.isc.common.config; package com.brilliance.isc.common.config;
import com.brilliance.isc.support.redis.RedisTemplateBuilder;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
...@@ -25,30 +26,8 @@ import java.util.List; ...@@ -25,30 +26,8 @@ import java.util.List;
@Configuration @Configuration
public class RedisTemplateConfig { public class RedisTemplateConfig {
@Bean("jacksonRedisTemplate") @Bean("jacksonRedisTemplate")
public RedisTemplate<String, Object> jacksonRedisTemplate(RedisConnectionFactory factory) { public RedisTemplate<String, Object> jacksonRedisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>(); return RedisTemplateBuilder.craeteJacksonRedisTemplate(factory);
template.setConnectionFactory(factory);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
jacksonSeial.setObjectMapper(objectMapper);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 设置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment