在使用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. MySQL编码问题探究

    占个坑. 今天在向本机搭建的MySQL数据库插入中文的时候报错了. 使用 show variables like 'char%'; 及 show variables like 'collation%' ...

  2. opencv的安装及填坑

    opencv的配置方式: https://blog.csdn.net/cocoaqin/article/details/78163171 输入Python时候报错: ERROR: ld.so: obj ...

  3. Unity shader学习之反射

    shader如下: Shader "Custom/Reflection" { Properties { _Cubemap("Cubemap", Cube) = ...

  4. 1.23 codeforces div3 C.Nice Garland

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  5. hdu5125 树状数组+dp

     hdu5125 他说的是n个人每个人都有两个气球a,b,气球各自都有相应的体积,现在让他们按照序号排列好来,对他们的a气球体积值计算最长上升子序列,对于这整个排列来说有m次机会让你将a气球替换成b气 ...

  6. Qt 事件机制

    [1]事件 事件是可以被控件识别的操作.如按下确定按钮.选择某个单选按钮或复选框. 每种控件有自己可识别的事件,如窗体的加载.单击.双击等事件,编辑框(文本框)的文本改变事件等等. 事件就是用户对窗口 ...

  7. 特征点方法 - Harris和SURF的手工实现

    整理去年做的小项目,纪念我的图像处理入门. 因为要在DSP上实现,所以完全手工C代码垒起来的,还要保证和PC端跑的结果一样,觉得可能特殊场景会有用,上传github,没有依赖任何库: 格式注释什么的暂 ...

  8. Linux基础命令---间歇执行命令watch

    watch watch指令可以间歇性的执行程序,将输出结果以全屏的方式显示,默认是2s执行一次.watch将一直运行,直到被中断. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS ...

  9. MongoDB优化,建立索引实例及索引机制原理讲解

    MongoDB优化,建立索引实例及索引机制原理讲解 为什么需要索引? 当你抱怨MongoDB集合查询效率低的时候,可能你就需要考虑使用索引了,为了方便后续介绍,先科普下MongoDB里的索引机制(同样 ...

  10. Centos7升级gcc学习笔记 gcc 4.8.5 -> gcc 5.4.0

    摘自:https://www.cnblogs.com/highway-9/p/5628852.html 一.安装开发必备环境: yum groupinstall "Development T ...