在使用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进行序列化的更多相关文章

  1. Springboot+Redis序列化坑

    今天在测试springboot整合redis的时候遇到下面这个坑,百度来百度去发现提示都是ajax的问题,真的是醉了,错误提示如下所示,不信大家可以直接复制百度一下答案是什么(流泪中....),错误如 ...

  2. Springboot+redis 整合

    运行环境: JDK1.7. SpringBoot1.4.7 redis3.0.4 1.生成Springboot项目,分别添加web,redis依赖,具体的maven依赖如下 <dependenc ...

  3. 补习系列(14)-springboot redis 整合-数据读写

    目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...

  4. SpringBoot+Redis整合

    SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...

  5. springboot +redis配置

    springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  6. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

  7. SpringBoot + Redis:基本配置及使用

    注:本篇博客SpringBoot版本为2.1.5.RELEASE,SpringBoot1.0版本有些配置不适用 一.SpringBoot 配置Redis 1.1 pom 引入spring-boot-s ...

  8. fastjson自定义序列化竟然有这么多姿势?

    本文介绍下fastjson自定义序列化的各种操作. 一.什么是fastjson? fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSO ...

  9. SpringBoot 03_利用FastJson返回Json数据

    自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...

随机推荐

  1. SpringBoot返回json格式到浏览器上,出现乱码问题

    在对应的Controller上的requestMapping中添加: @RestController@EnableAutoConfiguration@RequestMapping(value = &q ...

  2. JS实例2

    进度条 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8 ...

  3. 500次访问之后php-cgi进程退出

    之前遇到过php-cgi进程意外退出的问题,以为是负载过高导致的,这几天在写一个向数据库插数据的程序,测试稳定性时又遇到这种问题. 于是搜索,找到了这篇文章:http://stackoverflow. ...

  4. python 正则基本方法

    2017-04-11 学习python,免不了应对爬虫,初学爬虫最难理解的就是正则表达式. 比如我们要爬去网页上的某些内容,就像下面这种形式: <p>safdsf</p>< ...

  5. 找不到System.Web.Optimization命名空间

    找不到System.Web.Optimization命名空间,无法完成BundleConfig.cs内容的添加. 解决方法如下:打开程序包管理控制台,在控制台中输入:Install-PackageMi ...

  6. 【2017-2-20】C#运算符

    运算符分类: 1.算术运算符 ⑴+ - * / %(取余,模) /3; Console.Write(d); Console.ReadLine(); 则输出结果为“3”,因为10和3都是int型,dec ...

  7. mac下编译cpu only caffe并用xCode建caffe工程

    mac编译caffe 好像又变容易了,直接git clone下载blvc源码,make.config里去掉了CPU_ONLY前面的注释,并没有安装任何依赖,也可能是自己mac上本来有, xCode里调 ...

  8. base_review

    简述Python的字符串驻留机制. - 字符串驻留是一种仅保存一份相同且不可变字符串的方法. - 原理 - 系统维护interned字典,记录已被驻留的字符串对象. - 当字符串对象a需要驻留时,先在 ...

  9. sqlyog下载

    sqlyog下载(附注册码):http://www.onlinedown.net/soft/24926.htm

  10. java实现 HTTP/HTTPS请求绕过证书检测代码实现

    java实现 HTTP/HTTPS请求绕过证书检测代码实现 1.开发需求 需要实现在服务端发起HTTP/HTTPS请求,访问其他程序资源. 2.URLConnection和HTTPClient的比较 ...