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

springboot-redis使用fastjson2

1、pom

注:springboot2.*使用fastjson2-extension-spring5,3.*使用fastjson2-extension-spring6

<fastjson.version>2.0.37</fastjson.version>
<!-- json -->
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>${fastjson.version}</version>
</dependency>
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension</artifactId><version>${fastjson.version}</version>
</dependency>
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension-spring5</artifactId><version>${fastjson.version}</version>
</dependency>

2、RedisConfig

import com.alibaba.fastjson2.support.spring.data.redis.FastJsonRedisSerializer;
import lombok.extern.log4j.Log4j2;
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;@Log4j2
@Configuration
public class RedisConfig {@Autowiredprivate RedisConnectionFactory factory;@Bean(name = "redisTemplate")public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);//序列化//如需使用类自动化装配(redis直接存储类对象),请使用类FastJson2RedisSerializer序列化FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);// value值的序列化采用fastJsonRedisSerializertemplate.setDefaultSerializer(fastJsonRedisSerializer);log.info("redis client初始化完成");return template;}
}

3、FastJson2RedisSerializer

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import org.springframework.data.redis.serializer.RedisSerializer;public class FastJson2RedisSerializer<T> implements RedisSerializer<T> {static final Filter autoTypeFilter = JSONReader.autoTypeFilter(// 按需加上需要支持自动类型的类名前缀,范围越小越安全"com.***.***.***","com.***.***.***");private Class<T> clazz;public FastJson2RedisSerializer(Class<T> clazz) {super();this.clazz = clazz;}@Overridepublic byte[] serialize(T t) {if (t == null) {return new byte[0];}return JSON.toJSONBytes(t, JSONWriter.Feature.WriteClassName);}@Overridepublic T deserialize(byte[] bytes) {if (bytes == null || bytes.length <= 0) {return null;}return JSON.parseObject(bytes, clazz, autoTypeFilter);}
}
http://www.lryc.cn/news/97720.html

相关文章:

  • SOC FPGA之HPS模型设计(二)
  • Go基础—反射,性能和灵活性的双刃剑
  • MATLAB与ROS联合仿真(慕羽☆)全套开源资料索引
  • 三、深入浅出WPF之控件与布局
  • 社群积分运营策略:增加用户忠诚度
  • 推荐用于学习RN原生模块开发的开源库—react-native-ble-manager
  • MySQL中锁的简介——全局锁
  • RocketMQ集群4.9.2升级4.9.6版本
  • 具身智能controller---RT-1(Robotics Transformer)(上---方法介绍)
  • 视频内存过大如何压缩变小?这个压缩方法了解一下
  • 【Ansible】自动化部署工具-----Ansible
  • Ubuntu下安装Node.js;npm
  • 设计模式-模版方法模式
  • Linux 学习记录59(ARM篇)
  • TypeScript -- 函数
  • 网页开发基础——HTML
  • C# 继承,封装,多态等知识点
  • 决策树概述
  • 青枫壁纸小程序V1.4.0(后端SpringBoot)
  • Error: unknown flag: --export 【k8s,kubernets报错】
  • 进入linux系统中修改网段-ip
  • 通过REST API接口上传Nexus仓库
  • Docker镜像端口映射简介及配置指南
  • Excel双向柱状图的绘制
  • Linux6.17 Docker 安全及日志管理
  • 学好Elasticsearch系列-索引的CRUD
  • Python - OpenCV机器视觉库的简单使用经验
  • 【计算机网络 01】说在前面 信息服务 因特网 ISP RFC技术文档 边缘与核心 交换方式 定义与分类 网络性能指标 计算机网络体系结构 章节小结
  • POI信息点的diPointX、diPointY转化成经纬度
  • 虚拟机(VMware)安装Linux(Ubuntu)安装教程