SpringBoot Redis配置Fastjson进行序列化和反序列化实现
作者:码农云帆哥 发布时间:2023-10-09 04:45:57
标签:SpringBoot,Fastjson,序列化,反序列化
FastJson是阿里开源的一个高性能的JSON框架,FastJson数据处理速度快,无论序列化(把JavaBean对象转化成Json格式的字符串)和反序列化(把JSON格式的字符串转化为Java Bean对象),都是当之无愧的fast;功能强大(支持普通JDK类,包括javaBean, Collection, Date 或者enum);零依赖(没有依赖其他的任何类库)。
1、写一个自定义序列化类
/**
* 自定义序列化类
* @param <T>
*/
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (null == t) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (null == bytes || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
2、写一个Redis配置类
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//使用fastjson序列化
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
// value值的序列化采用fastJsonRedisSerializer
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
3、Student类
@Data
public class Student {
private Integer studentId;
private String studentName;
}
4、pom.xml引入redis和fastjson的依赖,application.yml配置文件别忘了配置Redis的地址。
5、BootRedisApplication启动类
@SpringBootApplication
public class BootRedisApplication {
public static void main(String[] args) {
ConfigurableApplicationContext
context = SpringApplication.run(BootRedisApplication.class, args);
Student student = new Student();
student.setStudentId(101);
student.setStudentName("学生A");
RedisTemplate cRedisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
cRedisTemplate.opsForValue().set("student-1", student);
context.close();
}
}
6、查看Redis的数据
{"@type":"com.example.bootredis.Student","studentId":101,"studentName":"学生A"}
来源:https://blog.csdn.net/sinat_27933301/article/details/102638698


猜你喜欢
- 01 高效设置我们先来讲讲有哪些设置调整之后 IDEA 会更好用。先说明一点:这里只是根据我个人喜好和习惯的建议,IDEA 的默认配置已经很
- jasperreport导出的pdf每页显示的记录太少主要是确保Details的高度与Details中Field Text的高度一致。jas
- 在处理网络请求时,有一部分功能是需要抽出来统一处理的,与业务隔开。登录校验可以利用spring mvc的 * Interceptor,实现H
- 本文实例为大家分享了Android自定义圆环倒计时控件的具体代码,供大家参考,具体内容如下先来一张最终效果图:主要思路: 在画渐变
- mybatis group by substr传参报错报异常### Cause: java.sql.SQLSyntaxErrorExcept
- 本文实例讲述了C#在RichTextBox中显示不同颜色文字的方法。分享给大家供大家参考。具体实现方法如下:#region 日志记录、支持其
- 目录一、前言二、正文2.1 注解2.1.1 注解1:@Target({ElementType.TYPE})2.1.2 注解2:@Retent
- 在我们开发过程中用 Mybatis 经常会用到下面的例子Mapper如下Map<String ,String > testArr
- 我们通常在使用Java 调用脚本的时候,会使用 Runtime 类如:// 打开浏览器并访问 http://localh
- 上篇随笔详细介绍了三种解析服务器端传过来的xml数据格式,而对于服务器端来说,返回给客户端的数据格式一般分为html、xml和json这三种
- 一. String对象的比较1. ==比较是否引用同一个对象注意:对于内置类型,==比较的是变量中的值;对于引用类型 , == 比较的是引用
- Android studio4.1更新后出现的问题如下> Task : app : kaptDebugKotlin FAILEDFAI
- 这个功能没什么可介绍的,大家都懂,直接上代码了。。实现功能选择多个文件压缩成ZIP文件和解压ZIP文件开发环境开发工具: Visual St
- 序列化序列化:将对象转换为二进制序列在网络中传输或保存到磁盘反序列化:从网络或磁盘中将二进制序列转换为对象注意:对象必须实现Serializ
- SpringBoot接收文件和对象使用场景:某个接口,需要同时接收文件和实体,也就是参数一、这个时候,前端就不能json格式传送数据了,要用
- int、String的类型转换int -> Stringint i=12345;String s="";第一种方法
- 业务场景我们知道在使用PageHelper分页插件时,会对执行PageHelper.startPage(pageNum, pageSize)
- 1.泛型概念泛型就是将类型参数化所谓类型参数化就是将类型定义成参数的形式,然后在使用此类型的时候的时候再传入具体的类型到这我们可以看出来:泛
- 在游戏项目中我们常常看到商城的广告牌,几张广告图片循环滚动,类似跑马灯,现在我将讨论一种实现方法,并提供一个管理类,大家可以直接使用。实现原
- Room其实就是一个orm,抽象了SQLite的使用,但是它作为Android的亲儿子orm,并且原生支持LiveData和Rxjava嵌套