redisTemplate 默认的序列化方式为 jdkSerializeable, StringRedisTemplate的默认序列化方式为StringRedisSerializer

可以通过手动配置, 将redisTemplate的序列化方式进行更改

package com.wenbronk.data.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; /**
* redis的启动类
* Created by wenbronk on 2017/6/12.
*/
@SpringBootApplication
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
} /**
* redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory); // 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

最新文章

  1. ERP入门
  2. Android学习笔记(十二)
  3. TestNG官方文档中文版(3)-testng.xml
  4. 使用ImageCreate()创建一个代表空白图像的变量
  5. RCNN--对象检测的又一伟大跨越 2(包括SPPnet、Fast RCNN)(持续更新)
  6. demo05
  7. Examples For When-Validate-Item trigger In Oracle Forms
  8. win10 phpStudy 80端口被占用
  9. 制作东皇3.2的安装U盘-黑苹果之路
  10. 在centos 6.5 在virtual box 上 安装增强版工具
  11. 【转】 xcode中常用快捷键图文并茂解释
  12. Setfocus - IE 需要使用setTimeout
  13. 自定义toast功能
  14. 文件操作IO流
  15. android方向键被锁定的问题
  16. Windows进程间通信(上)
  17. 美团点评DBProxy读写分离使用说明
  18. 开源 .net license tool, EasyLicense !
  19. AHA高级心血管生命支持ACLS课前自我评估测试
  20. Day4--Python--列表增删改查,元组,range

热门文章

  1. OpenGl 坐标转换 (转载)
  2. MyEclipse2014中Java类右键Run as没有JUnit Test
  3. spring mvc学习笔记(一)web.xml文件配置的一点重要信息
  4. Microsoft SQL Server 2012 管理 (2): 实例与数据库管理
  5. linux系统编程:setjmp和longjmp函数用法
  6. python3--django for 循环中,获取序号
  7. K8S+GitLab-自动化分布式部署ASP.NET Core(一) 部署环境
  8. 使用客户端软件向服务端php程序发送post数据,php接受三种方法
  9. 消息中间件(Kafka/RabbitMQ)收录集
  10. 实现liunx之间无密码访问——ssh密匙