SpringBoot 2.3.5 整合redis缓存自定义JSON序列化中遇到的问题。
2020-06-25 00:54
645 查看
目前正在学习尚硅谷雷丰阳老师的springboot的课程。
老师课程中使用的springboot的版本是1.5,而我使用的是2.3.5。
springboot的2.x版本与1.x版本的差别还蛮大的,很多底层代码都改变了,而且变化还蛮大了。
当前我正在学习利用redis缓存自定义JSON序列化,遇到了2.x底层源码的配置自定义JSON序列元与1.x(雷老师课程中讲授的)有很大的不同的问题。
花费大量的时间百度后,尝试了n种方法后,终于找到了一种可行的方法,特此记录,以便其他遇到此问题的朋友以及自己今后的复用。
我参考的这位大神的文章:https://www.geek-share.com/detail/2802219775.html
其中最关键的是配置redis的配置类来自定义JSON序列化,代码如下:我把我的导的包也写进来了。
package com.atguigu.cache.config; import com.atguigu.cache.bean.Employee; import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.net.UnknownHostException; import java.time.Duration; @Configuration public class MyRedisConfig { @Bean public RedisTemplate<Object, Employee> EmpRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class); template.setDefaultSerializer(serializer); return template; } //CacheManagerCustomizers可以来定制缓存的一些规则 @Bean public CacheManager cacheManager(RedisConnectionFactory factory){ RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofDays(1)) .disableCachingNullValues() .serializeKeysWith(RedisSerializationContext.SerializationPair .fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();} }
这个方法是亲测可行的,但是也有一个小坑,我卡在这了快半个钟。
如果之前 没有配置自定义的CacheManager,就会使用默认的配置,利用Jdk来序列化,存入redis中。
在之前操作过的情况下,redis中就会保留此次记录,当再次运行后,就会报错,报错信息如下:
Failed to complete request: org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unexpected character ('¬' (code 172)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
这个报错的原因就是因为之前在redis中以当前使用的key 缓存了一个以默认jdk来序列化的值,所以导致此次缓存的失败。
只要到redis中把之前用这个key来缓存的默认jdk序列化给删除了,就可以了。
现在再回头来看,如此简单的几步操作,前前后后竟卡了我一个多小时。
故决定写下此文,毕竟好记性不如烂笔头,也方便其他正在学习springboot的朋友们!
相关文章推荐
- SpringBoot 2.3 整合redis缓存自定义序列化
- Spring boot和Dubbot整合的小项目遇到的问题和解决方法
- springboot整合redis并实现自定义序列化规则
- springboot整合redis,需要自定义redisTemplate模板。采用的Jackson2JsonRedisSerializer序列化,拿来即用
- SpringBoot 整合 Shiro 过程中遇到奇怪的类型转换问题
- 使用idea学习SpringBoot整合Mybatis,遇到mapper无法注入的问题
- Spring boot整合mybatis puls遇到的问题
- Spring-Boot整合通用PageHelper插件遇到的问题
- SpringBoot整合Redis读写JSON序列化格式
- SpringBoot整合activiti遇到的一些问题
- springboot中整合activiti流程设计器时遇到访问项目需要输入tomcat用户名和密码的问题
- SpringBoot @JsonDeserialize自定义Json序列化
- IDEA创建一个spring-boot整合Mybatis的模块时,遇到的问题
- spring boot整合mybatis测试时遇到的问题
- springboot中自定义配置遇到的问题
- springboot整合mybatis遇到的问题
- spring boot整合多数据源遇到的问题
- 总结一下springboot项目整合log4j2框架过程中遇到的问题
- sts springboot整合freemarker 遇到的问题
- SpringBoot整合Thymeleaf遇到的问题