SpringBoot Redis使用fastjson进行序列化
在使用spring-data-redis,默认情况下是使用org.springframework.data.redis.serializer.JdkSerializationRedisSerializer这个类来做序列化
我们使用jackson方式:
Jackson redis序列化是spring中自带的
@Bean(name="redisTemplate") public RedisTemplate<String, Object> redisTemplate() { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.setDefaultSerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; }
使用fastjson方式:
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private Class<T> clazz; public FastJson2JsonRedisSerializer(Class<T> clazz) { super(); this.clazz = clazz; } public byte[] serialize(T t) throws SerializationException { if (t == null) { return new byte[0]; } return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); } public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length <= 0) { return null; } String str = new String(bytes, DEFAULT_CHARSET); return (T) JSON.parseObject(str, clazz); } }
注册:
@Configuration public class RedisConfig { @Autowired private RedisConnectionFactory factory; @Autowired private RedisSerializer fastJson2JsonRedisSerializer; //fastjson @Bean(name="redisTemplate") public RedisTemplate<String, Object> fastJsonRedisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); //redis开启事务 template.setEnableTransactionSupport(true); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(fastJson2JsonRedisSerializer); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(fastJson2JsonRedisSerializer); template.setDefaultSerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; } }
在redis工具类中调用RedisTemplate:
@Component public class RedisCacheUtil { @Autowired @Qualifier("redisTemplate") private RedisTemplate<String, String> redisTemplate; }
对比:
jackson方式序列化存储redis中数据:
[ "com.qhong.test.dependBean.Person", { "age": 20, "name": "name0", "iss": true } ]
[ "java.util.ArrayList", [ [ "com.qhong.test.dependBean.Person", { "age": 20, "name": "name0", "iss": true } ], [ "com.qhong.test.dependBean.Person", { "age": 21, "name": "name1", "iss": true } ], [ "com.qhong.test.dependBean.Person", { "age": 22, "name": "name2", "iss": true } ] ] ]
上面的完全不符合json格式规范
fastjson方式序列化:
{ "@type": "com.qhong.test.dependBean.Person", "age": 20, "iss": true, "name": "name0" }
[ { "@type": "com.qhong.test.dependBean.Person", "age": 20, "iss": true, "name": "name0" }, { "@type": "com.qhong.test.dependBean.Person", "age": 21, "iss": true, "name": "name1" }, { "@type": "com.qhong.test.dependBean.Person", "age": 22, "iss": true, "name": "name2" } ]
虽然也不是很好,但是比jackson的好多了
分类:
Redis
, SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)