SpringBoot Redis配置Fastjson進(jìn)行序列化和反序列化實(shí)現(xiàn)
FastJson是阿里開源的一個(gè)高性能的JSON框架,F(xiàn)astJson數(shù)據(jù)處理速度快,無論序列化(把JavaBean對(duì)象轉(zhuǎn)化成Json格式的字符串)和反序列化(把JSON格式的字符串轉(zhuǎn)化為Java Bean對(duì)象),都是當(dāng)之無愧的fast;功能強(qiáng)大(支持普通JDK類,包括javaBean, Collection, Date 或者enum);零依賴(沒有依賴其他的任何類庫)。
1、寫一個(gè)自定義序列化類
/**
* 自定義序列化類
* @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、寫一個(gè)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啟動(dòng)類
@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("學(xué)生A");
RedisTemplate cRedisTemplate = context.getBean("redisTemplate", RedisTemplate.class);
cRedisTemplate.opsForValue().set("student-1", student);
context.close();
}
}
6、查看Redis的數(shù)據(jù)
{"@type":"com.example.bootredis.Student","studentId":101,"studentName":"學(xué)生A"}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決springboot與springcloud版本兼容問題(附版本兼容表)
在基于spring boot搭建spring cloud時(shí),創(chuàng)建eureka后啟動(dòng)服務(wù)發(fā)生報(bào)錯(cuò),本文給大家介紹了解決springboot與springcloud版本兼容問題的幾種方案,需要的朋友可以參考下2024-02-02
Java實(shí)現(xiàn)簡單圖形界面計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單圖形界面計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
spring中Mapstruct屬性映射的實(shí)現(xiàn)
本文主要介紹了spring中Mapstruct屬性映射的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Jmeter連接Mysql數(shù)據(jù)庫實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Jmeter連接Mysql數(shù)據(jù)庫實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
分析Java非阻塞算法Lock-Free的實(shí)現(xiàn)
非阻塞算法一般會(huì)使用CAS來協(xié)調(diào)線程的操作。雖然非阻塞算法有諸多優(yōu)點(diǎn),但是在實(shí)現(xiàn)上要比基于鎖的算法更加繁瑣和負(fù)責(zé)。本文將會(huì)介紹兩個(gè)是用非阻塞算法實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)。2021-06-06

