Redis使⽤FastJson序列化FastJson2JsonRedisSerializer 背景
最近在⽤SpringBoot+Redis+SpringCache做个缓存。但是发现⽹上很多默认都是⽤的jackson序列化,那是多么古⽼,fastjson才是王道,所以这才有了这个FastJson2JsonRedisSerializer。
FastJson2JsonRedisSerializer.java
⽹上的fastjson似乎都⼀般般,这个是⽹上的⼀个加强版
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.pe.TypeFactory;
import org.dis.serializer.RedisSerializer;
import org.dis.serializer.SerializationException;
import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* FastJson2JsonRedisSerializer
*  Redis使⽤FastJson序列化
*  by zhengkai.blog.csdn
*/
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
private ObjectMapper objectMapper = new ObjectMapper();
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
static {
//如果遇到反序列化autoType is not support错误,请添加并修改⼀下包名到bean⽂件路径
// GlobalInstance().addAccept("");
}
public FastJson2JsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
JSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz);
}
public void setObjectMapper(ObjectMapper objectMapper) {
}
protected JavaType getJavaType(Class<?> clazz) {
return TypeFactory.defaultInstance().constructType(clazz);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44fastjson怎么用
44
45
46
47
48
49
50
51
52
53
54
55
56
57
RedisConfig
⽤fastjson来序列化value的值,⽤string来序列化key的值。
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {        RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使⽤Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
//Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
//使⽤Fastjson2JsonRedisSerializer来序列化和反序列化redis的value值 by zhengkai
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使⽤StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"autoType is not support"问题
⽤tomcat+nginx+redis在负载均衡情况下做使⽤fastjson反序列化可能会遇到
com.alibaba.fastjson.JSONException: autoType is not support
原因是是fastjson默认是开启的了autoType,只需要在FastJson2JsonRedisSerializer⾥⾯设置⼀个静态变量添加bean/domain/entity 的对应包名进⾏⽩名单排除即可。感谢@Alan⽼三 的反馈。
static {
}
1
2
3
或者使⽤setAutoTypeSupport=true的全局设置
static {
}