Spring Data Redis入门示例:数据序列化 (四)
概述
RedisTemplate默认使用的是基于JDK的序列化器,所以存储在Redis的数据如果不经过相应的反序列化,看到的结果是这个样子的:

可以看到,出现了乱码,在程序层面上,不会影响程序的运行,但当出现数据错误,对数据进行排查时,就无从下手了。
序列化器
在Spring Data Redis中,用户自定义类型和存储数据之间的转换(反之亦然)由org.springframework.data.redis.serializer包下的类进行处理。
这个包包含两种类型的序列化程序,它们负责序列化过程:
- 基于
RedisSerializer的双向序列化器 - 通过
RedisElementReader和RedisElementWriter进行元素的读写(2.0引进的)
这两种类型的序列化器主要区别在于,RedisSerializer主要序列化为byte[],RedisElementReader/RedisElementWriter使用ByteBuffer。
基于RedisSerializer的序列化器有多种实现,可以直接使用,如: StringRedisSerializerJdkSerializationRedisSerializer(默认)OxmSerializer(依赖于Spring OXM)Jackson2JsonRedisSerializer/GenericJackson2JsonRedisSerializer(依赖于jackson)
序列化的范围
序列化可以应用到 Key , Value , HashKey , HashValue 上,在 RedisTemplate 中,有四个属于与之对应:
keySerializer 、 valueSerializer 、 hashKeySerializer 、 hashValueSerializer
基于Json的序列化
引入Jackson的maven依赖:
<jackson.databind.version>2.8.5</jackson.databind.version>
<!-- jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind.version}</version>
</dependency>
配置序列化器:
<!-- 缓存序列化方式 -->
<!--对key的默认序列化器。默认值是StringSerializer -->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!--是对value的默认序列化器,默认值是取自DefaultSerializer的JdkSerializationRedisSerializer。 -->
<bean id="genericJackson2JsonRedisSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
重新配置RedisTemplate:
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer"/>
<property name="hashKeySerializer" ref="stringRedisSerializer"/>
<property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer" />
</bean>
重新运行上一节的示例,得到如下结果:

示例说明
1. 查看redis的管理工具是:RedisDesktopManager,下载地址:https://redisdesktop.com/download
Spring Data Redis入门示例:数据序列化 (四)的更多相关文章
- Spring Data Redis入门示例:基于Jedis及底层API (二)
使用底层API:RedisConnectionFactory和RedisConnection可以直接操作Redis,下面是一个简单的例子: ### Maven依赖 <properties> ...
- Spring Data Redis入门示例:Hash操作(七)
将对象存为Redis中的hash类型,可以有两种方式,将每个对象实例作为一个hash进行存储,则实例的每个属性作为hash的field:同种类型的对象实例存储为一个hash,每个实例分配一个field ...
- Spring Data Redis入门示例:字符串操作(六)
Spring Data Redis对字符串的操作,封装在了ValueOperations和BoundValueOperations中,在集成好了SPD之后,在需要的地方引入: // 注入模板操作实例 ...
- Spring Data Redis入门示例:基于RedisTemplate (三)
使用底层API:RedisConnection操作Redis,需要对数据进行手动转换(String <---->byte),需要进行多数重复性工作,效率低下:org.springframe ...
- Spring Data Redis入门示例:程序配置(五)
单机配置 redis.properties配置 #redis的服务器地址 redis.host=127.0.0.1 #redis的服务端口 redis.port=6379 #客户端超时时间单位是毫秒 ...
- spring data redis使用示例
1. 配置依赖文件 <dependencies> <dependency> <groupId>org.springframework.data</groupI ...
- 关于Spring Data redis几种对象序列化的比较
redis虽然提供了对list set hash等数据类型的支持,但是没有提供对POJO对象的支持,底层都是把对象序列化后再以字符串的方式存储的.因此,Spring data提供了若干个Seriali ...
- Spring Data Redis —— 快速入门
环境要求:Redis 2.6及以上,javase 8.0及以上: 一.Spring Data Redis 介绍 Spring-data-redis是spring的一部分,提供了在srping应用中通过 ...
- Spring Data Redis示例
说明 关于Redis:一个基于键值对存储的NoSQL内存数据库,可存储复杂的数据结构,如List, Set, Hashes. 关于Spring Data Redis:简称SDR, 能让Spring应用 ...
随机推荐
- ChartCtrl源码剖析之——CChartLegend类
CChartLegend类用来绘制每一个波形的描述信息,它处于该控件的区域,如下图所示: CChartLegend类的头文件. #if !defined(AFX_CHARTLEGEND_H__CD72 ...
- E20180324-hm
popover 弹出框 archive v. 存档; n. 档案文件; 档案室;
- HDU-ACM“菜鸟先飞”冬训系列赛——第10场
Problem A 题意 给出l(房子宽度),d(pole距离房子的垂直距离),s(绳子长度),求可覆盖的面积 分析 一共四种情况 \[1.s<=d\] \[2.s<=sqrt(d*d+l ...
- bzoj 4195: [Noi2015]程序自动分析【并查集】
等于有传递性,所以hash一下把等于用并查集连起来,然后再判断不等于是否合法即可 #include<iostream> #include<cstdio> #include< ...
- 贪心 CodeForces 124B Permutations
题目传送门 /* 贪心:全排列函数使用,更新最值 */ #include <cstdio> #include <algorithm> #include <cstring& ...
- 思维题 Gym 100553A Alter Board
题目传送门 /* 题意:一个n×m的矩形,相邻的颜色不同,黑或白.问最少的翻转次数,每次翻转可指定任意一个子矩形 思维题:最少要把偶数行和列翻转,也就是n/2+m/2次 */ #include < ...
- magento 用程序生成优惠劵码
参考自http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code 上面的代码只能生成C ...
- hbase最近的一些实践
有一段实践没有写东西了,最近组里面来了两个新的小伙伴,并且一起针对目前的hbase集群做了一些运维和优化实践,比较零散,记录下来供以后以及和大家参考. 1,hbase regionserver宕机导致 ...
- 微信里去掉下拉select的边框
<select name="gender" id="" class=" " style=" -webkit-appeara ...
- [转]访问 OData 服务 (WCF Data Services)
本文转自:http://msdn.microsoft.com/zh-SG/library/dd728283(v=vs.103) WCF 数据服务 支持开放式数据协议 (OData) 将数据作为包含可通 ...