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[];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= ) {
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": ,
"name": "name0",
"iss": true
}
]
[
"java.util.ArrayList",
[
[
"com.qhong.test.dependBean.Person",
{
"age": ,
"name": "name0",
"iss": true
}
],
[
"com.qhong.test.dependBean.Person",
{
"age": ,
"name": "name1",
"iss": true
}
],
[
"com.qhong.test.dependBean.Person",
{
"age": ,
"name": "name2",
"iss": true
}
]
]
]
上面的完全不符合json格式规范
fastjson方式序列化:
{
"@type": "com.qhong.test.dependBean.Person",
"age": ,
"iss": true,
"name": "name0"
}
[
{
"@type": "com.qhong.test.dependBean.Person",
"age": ,
"iss": true,
"name": "name0"
},
{
"@type": "com.qhong.test.dependBean.Person",
"age": ,
"iss": true,
"name": "name1"
},
{
"@type": "com.qhong.test.dependBean.Person",
"age": ,
"iss": true,
"name": "name2"
}
]
虽然也不是很好,但是比jackson的好多了
https://www.jianshu.com/p/138f3713618a
https://github.com/haha174/seckill
SpringBoot Redis使用fastjson进行序列化的更多相关文章
- Springboot+Redis序列化坑
今天在测试springboot整合redis的时候遇到下面这个坑,百度来百度去发现提示都是ajax的问题,真的是醉了,错误提示如下所示,不信大家可以直接复制百度一下答案是什么(流泪中....),错误如 ...
- Springboot+redis 整合
运行环境: JDK1.7. SpringBoot1.4.7 redis3.0.4 1.生成Springboot项目,分别添加web,redis依赖,具体的maven依赖如下 <dependenc ...
- 补习系列(14)-springboot redis 整合-数据读写
目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...
- SpringBoot+Redis整合
SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...
- springboot +redis配置
springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- SpringBoot + Redis:基本配置及使用
注:本篇博客SpringBoot版本为2.1.5.RELEASE,SpringBoot1.0版本有些配置不适用 一.SpringBoot 配置Redis 1.1 pom 引入spring-boot-s ...
- fastjson自定义序列化竟然有这么多姿势?
本文介绍下fastjson自定义序列化的各种操作. 一.什么是fastjson? fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSO ...
- SpringBoot 03_利用FastJson返回Json数据
自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...
随机推荐
- Springboot整合Mybatis 之分页插件使用
1: 引入jar包 <!-- 引入MyBatis分页插件--> <dependency> <groupId>com.github.pagehelper</gr ...
- Intel 80386 微处理器的存储器管理
一.存储器的管理 存储器的管理是一种硬件机制,微处理器在总线地址上对物理存储器进行寻址.但是,为了给程序提供比物理存储器容量更大的空间,就引入了虚拟存储器的概念,它在外存(比如磁盘)的支持 ...
- html5-相对定位
*{ margin: 0px; padding: 0px;}div{ width: 300px; height: 300px;}#div1{ background: rg ...
- Latex数学公式中的空格
http://blog.sina.com.cn/s/blog_4ddef8f80100iwwv.html 两个quad空格 a \qquad b 两个m的宽度 quad空格 a \quad b 一个m ...
- Sql server 存储过程批量插入若干数据。
测试时,经常需要生成大量数据来测试系统性能,此功能可以用存储过程快速生成. 1. 随机生成日期 DECLARE @Date_start datetime DECLARE @Date_end datet ...
- jQuery选择器--:eq(index)、:lt(index)和:gt(index)
:eq(index) 概述 匹配一个给定索引值的元素 参数 index 从 0 开始计数 :gt(index) 概述 匹配所有大于给定索引值的元素 参数 index 从 0 开始计数 ...
- 开发vue单页面Demo
第1步:安装webpack脚手架 npm install webpack -g (全局安装) (新电脑启动npm run dev版本报错,是因为webpack-server版本更新的问题,要安装pac ...
- RN与android原生开发混合后的环境报错问题
RN与android原生开发混合后的环境报错问题 需要先安装nodejs$ yarn --version1.12.1更新当前版本yarn upgrade --latest安装 | Yarnhttps: ...
- 【JavaScript 6连载】六、认识原型
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- JustOj 1974: 简单的事情 (组合数)
题目描述 数学天才fans曾经说过一句话:组合数的计算是一件非常简单的事情.组合数的计算真的是一件非常简单的事情吗?请你自己去尝试一下吧! 输入 输入中的一些整数对n,m(m≤n≤20) 输出 输出其 ...