当前位置: 首页 > news >正文

SpringBoot集成Redis,使用RedisTemple存储对象使用纯JSON格式

SpringBoot集成Redis,使用RedisTemple存储对象使用纯JSON格式

1、对象使用Json序列化

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;import java.nio.charset.Charset;public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");private Class<T> clazz;public FastJsonRedisSerializer(Class<T> clazz) {super();this.clazz = clazz;}@Overridepublic byte[] serialize(T t) throws SerializationException {if (null == t) {return new byte[0];}if (t instanceof String) {return t.toString().getBytes();}//SerializerFeature.NotWriteRootClassName 设置存储对象使用json,不带对象包路径信息return JSON.toJSONString(t, SerializerFeature.NotWriteRootClassName).getBytes(DEFAULT_CHARSET);}@Overridepublic T deserialize(byte[] bytes) throws SerializationException {if (null == bytes || bytes.length <= 0) {return null;}String str = new String(bytes, DEFAULT_CHARSET);//是否打开autoTypeParserConfig.getGlobalInstance().setAutoTypeSupport(false);return JSON.parseObject(str, clazz);}
}

创建RedisTemple对象

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(@Autowired(required = false) RedisConnectionFactory redisConnectionFactory) {ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(fastJsonRedisSerializer);redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}
}
http://www.lryc.cn/news/348059.html

相关文章:

  • [muduo网络库]——muduo库的Reactor模型(剖析muduo网络库核心部分、设计思想)
  • vue中怎样清除computed的缓存
  • 代码大师的工具箱:现代软件开发利器
  • 整理好了!2024年最常见 100 道 Java基础面试题(四十三)
  • 【TypeScript模块简介以及使用方法】
  • Offer必备算法38_贪心算法四_八道力扣题详解(由易到难)
  • java8 转对象,Java8转Map,Java8转Llist
  • 【Pytest官方文档翻译及学习】2.1 如何调用pytest
  • RabbitMQ的用途
  • R语言软件安装及配置
  • 网络配置的加密存储
  • 你写代码,会关注时间复杂度吗?
  • 【连连国际注册/登录安全分析报告】
  • linux进阶高级配置,你需要知道的有哪些(10)-远程访问
  • 不显示 表格 style=“display: none;“ 这个默认是不显示的
  • Bittensor怎么挖?手把手教你,使用bitget钱包
  • 领略Java内部类的“内部”
  • PHP 提取数组中的特定的值
  • SpringBoot、JAVA中excel、rtf、doc转PDF
  • 生信技能45 - 基于docker容器运行生信软件
  • 算法训练营第63天|LeetCode 84.柱状图中最大的矩形
  • python跟C++选哪个?
  • 速锐得深入解析吉利几何CAN总线数据通信网络的拓扑层级框架技术
  • 数据分析的数据模型
  • SQL注入-通达OA SQL注入漏洞【CVE-2023-4166】原理及检测思路分析
  • 数据结构(七)复杂度渐进表示
  • 3d如何同时贴两个图在模型上?---模大狮模型网
  • 【全开源】Java同城预约月嫂服务上门服务本地服务源码APP+小程序+公众号+H 5
  • 汇聚荣科技:拼多多开店时后期押金可以退吗?
  • 【机器学习与实现】K近邻算法