SpringBoot Redis配置Fastjson进行序列化和反序列化实现

作者:码农云帆哥 时间:2023-10-09 04:45:57 

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

标签:SpringBoot,Fastjson,序列化,反序列化
0
投稿

猜你喜欢

  • SpringMVC请求参数的使用总结

    2022-11-30 22:23:18
  • Java数据结构超详细分析二叉搜索树

    2022-12-01 01:34:20
  • spring boot 配置动态刷新详解

    2023-09-26 10:24:42
  • Android实现使用微信登录第三方APP的方法

    2021-06-09 00:05:02
  • Android View移动的3种方式总结

    2022-04-29 02:04:47
  • Java中多媒体文件上传及页面回显的操作代码

    2021-11-21 09:45:31
  • executor包执行器功能

    2023-07-26 21:07:36
  • Android自定义软键盘的设计与实现代码

    2023-12-19 11:57:01
  • C#程序中session值的保存方法以及转为字符串的方法总结

    2023-04-21 03:44:59
  • Springboot启动后执行方法小结

    2022-09-26 22:12:02
  • Android 使用Vitamio打造自己的万能播放器(10)—— 本地播放 (缩略图、视频信息、视频扫描服务)

    2023-07-11 08:48:22
  • Java实现医院管理系统

    2023-11-22 18:36:07
  • java的package和import机制原理解析

    2021-07-20 08:19:05
  • java字符串常用操作方法(查找、截取、分割)

    2023-11-29 03:21:13
  • servlet异步请求的实现

    2023-07-14 17:11:38
  • 不规范使用ThreadLocal导致bug分析解决

    2023-11-24 20:03:59
  • Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)

    2022-07-31 14:39:23
  • springboot以FTP方式上传文件到远程服务器

    2022-10-13 06:19:27
  • C语言文件操作之fread函数详解

    2023-07-06 18:24:15
  • java 代码块与静态代码块加载顺序

    2021-08-01 15:19:35
  • asp之家 软件编程 m.aspxhome.com