SpringBoot中如何配置Redis使用Fastjson进行高效序列化与反序列化操作?

2026-06-10 15:38:43 623阅读 0评论 SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计444个文字,预计阅读时间需要2分钟。

FastJson是阿里开源的高性能JSON框架,数据解析速度快,无需序列化,直接将JavaBean对象转换为Json格式的字符串,以及反序列化(将JSON格式的字符串转换为JavaBean对象)。

SpringBoot中如何配置Redis使用Fastjson进行高效序列化与反序列化操作?

FastJson是阿里开源的一个高性能的JSON框架,FastJson数据处理速度快,无论序列化(把JavaBean对象转化成Json格式的字符串)和反序列化(把JSON格式的字符串转化为Java Bean对象),都是当之无愧的fast;功能强大(支持普通JDK类,包括javaBean, Collection, Date 或者enum);零依赖(没有依赖其他的任何类库)。

SpringBoot中如何配置Redis使用Fastjson进行高效序列化与反序列化操作?

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"}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签: 高效 序列 化与

本文共计444个文字,预计阅读时间需要2分钟。

FastJson是阿里开源的高性能JSON框架,数据解析速度快,无需序列化,直接将JavaBean对象转换为Json格式的字符串,以及反序列化(将JSON格式的字符串转换为JavaBean对象)。

SpringBoot中如何配置Redis使用Fastjson进行高效序列化与反序列化操作?

FastJson是阿里开源的一个高性能的JSON框架,FastJson数据处理速度快,无论序列化(把JavaBean对象转化成Json格式的字符串)和反序列化(把JSON格式的字符串转化为Java Bean对象),都是当之无愧的fast;功能强大(支持普通JDK类,包括javaBean, Collection, Date 或者enum);零依赖(没有依赖其他的任何类库)。

SpringBoot中如何配置Redis使用Fastjson进行高效序列化与反序列化操作?

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"}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签: 高效 序列 化与