SpringBoot2.0 集成Redis @Cacheable乱码或redis LocalDateTime序列化 cacheManager和redisTemplate
在使用SpringBoot1.x的时候,通过简单的配置RedisTemplete就可以了,升级到SpringBoot2.0,spring-boot-starter-data-redis也跟着升起来了,
@Cacheable就出现了乱码的情况,可以通过如下配置方式解决
@Configuration public class Config extends CachingConfigurerSupport { @Bean(name="redisTemplate") public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, String> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); 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.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); // 配置序列化 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(redisCacheConfiguration) .build(); return cacheManager; } }
redis 里面LocalDateTime 字段序列化问题
@JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class)
redis 里面LocalDateTime 字段序列化问题 全局配置
@Autowired private LettuceConnectionFactory lettuceConnectionFactory; @Bean public KeyGenerator keyGenerator() {//设置自定义key{ClassName + methodName + params} return (target, method, params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(",Method:"); sb.append(method.getName()); sb.append(",Params["); for (int i = 0; i < params.length; i++) { sb.append(params[i].toString()); if (i != (params.length - 1)) { sb.append(","); } } sb.append("]"); return sb.toString(); }; } //自定义keyGenerator,Key生成器 @Bean public KeyGenerator updateByIdkeyGenerator() { return (target, method, params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(",Method:"); sb.append("getById"); sb.append(",Params["); try { Field id = params[0].getClass().getDeclaredField("id"); id.setAccessible(true); sb.append(id.get(params[0]).toString()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } sb.append("]"); return sb.toString(); }; } //自定义keyGenerator,Key生成器 @Bean public KeyGenerator deleteByIdkeyGenerator() { return (target, method, params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(",Method:"); sb.append("getById"); sb.append(",Params["); for (int i = 0; i < params.length; i++) { sb.append(params[i].toString()); if (i != (params.length - 1)) { sb.append(","); } } sb.append("]"); return sb.toString(); }; } @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper om = new ObjectMapper(); // 解决jackson2无法反序列化LocalDateTime的问题 //LocalDatetime序列化 JavaTimeModule timeModule = new JavaTimeModule(); timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); om.registerModule(timeModule); jackson2JsonRedisSerializer.setObjectMapper(om); //配置序列化 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)); // //设置缓存过期时间 // if (expireTime > 0) { // log.info("Redis 缓存过期时间 : {}", expireTime); // //设置缓存有效期 秒 // redisCacheConfiguration.entryTtl(Duration.ofSeconds(expireTime)); // } else { // log.info("Redis 未设置缓存过期时间"); // } return RedisCacheManager .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)) .cacheDefaults(redisCacheConfiguration).build(); } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {//创建RedisTemplate // 设置序列化 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( Object.class); ObjectMapper om = new ObjectMapper(); //LocalDatetime序列化 JavaTimeModule timeModule = new JavaTimeModule(); timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); om.registerModule(timeModule); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); //过时写法 //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); RedisSerializer<?> stringSerializer = new StringRedisSerializer(); // key序列化 redisTemplate.setKeySerializer(stringSerializer); // value序列化 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // Hash key序列化 redisTemplate.setHashKeySerializer(stringSerializer); // Hash value序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; }
参考链接 https://blog.csdn.net/cfm_gavin/article/details/107560379
https://www.shangmayuan.com/a/f013e195971b46cb8308b819.html 解决redis序列化java8 LocalDateTime
早年同窗始相知,三载瞬逝情却萌。年少不知愁滋味,犹读红豆生南国。别离方知相思苦,心田红豆根以生。