redis使用fastjson2序列化配置
将java object作为value存入redis时,原生redis序列化存在问题,无法存入,下面使用fastjson2进行配置解决
一些依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.56</version>
</dependency>
FastJson2RedisSerializer
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import org.springframework.data.redis.serializer.RedisSerializer;
public class FastJson2RedisSerializer<T> implements RedisSerializer<T> {
static final Filter autoTypeFilter = JSONReader.autoTypeFilter(
// 按需加上需要支持自动类型的类名前缀,范围越小越安全
"com.***.***.***","com.***.***.***"
);
private Class<T> clazz;
public FastJson2RedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) {
if (t == null) {
return new byte[0];
}
return JSON.toJSONBytes(t, JSONWriter.Feature.WriteClassName);
}
@Override
public T deserialize(byte[] bytes) {
if (bytes == null || bytes.length <= 0) {
return null;
}
return JSON.parseObject(bytes, clazz, autoTypeFilter);
}
}
RedisConfig
import com.cxy.cbms.common.utils.FastJson2RedisSerializer;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Log4j2
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;
@Bean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//序列化
//如需使用类自动化装配(redis直接存储类对象),请使用类FastJson2RedisSerializer序列化
FastJson2RedisSerializer<Object> fastJsonRedisSerializer = new FastJson2RedisSerializer<>(Object.class);
// value值的序列化采用fastJsonRedisSerializer
template.setDefaultSerializer(fastJsonRedisSerializer);
log.info("redis client初始化完成");
return template;
}
}
RedisService.java
封装方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* 设置键值对,并指定过期时间
*
* @param key 键
* @param value 值
* @param expire 过期时间(单位:秒)
*/
public void setValueWithExpire(Object key, Object value, long expire) {
redisTemplate.opsForValue().set(key, value); // 设置键值对
redisTemplate.expire(key, expire, TimeUnit.SECONDS); // 设置过期时间
}
/**
* 获取键的值
*
* @param key 键
* @return 值
*/
public Object getValue(Object key) {
return redisTemplate.opsForValue().get(key);
}
}
application.properties
# Redis 配置
spring.data.redis.host=localhost
spring.data.redis.port=6379
#可选需要密码验证
spring.data.redis.password=yourpassword

浙公网安备 33010602011771号