• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

springboot 搞代码 4年前 (2022-01-05) 23次浏览 已收录 0个评论

这篇文章主要介绍了SpringBoot Redis配置Fastjson进行序列化和反序列化实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

1、写一个自定义序列化类

 /** * 自定义序列化类 * @param  */ public class FastJsonRedisSerializer implements RedisSerializer { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private Class clazz; public FastJsonRedisSerializer(Class 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<strong style="color:transparent">来源gao@daima#com搞(%代@#码网</strong> str = new String(bytes, DEFAULT_CHARSET); return (T) JSON.parseObject(str, clazz); } }

2、写一个Redis配置类

 @Configuration public class RedisConfiguration { @Bean public RedisTemplate redisTemplate( RedisConnectionFactory redisConnectionFactory) { RedisTemplate 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"}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是SpringBoot Redis配置Fastjson进行序列化和反序列化实现的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot Redis配置Fastjson进行序列化和反序列化实现

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址