这几天用tomcat、nginx、redis配置socket的负载均衡在做信息共享的使用fastjson反序列化遇到了个啃爹的事情
com.alibaba.fastjson.JSONException: autoType is not support
网上查了下这个错误的原因,发现有很多人也遇到了。
原来是fastjson默认是开启的了autoType
只要在我们新建的GenericFastJson2JsonRedisSerializer里面添加白名单既可
贴上GenericFastJson2JsonRedisSerializer代码
/**
* Created by DESTINY on 2018/11/7.
*/
public class GenericFastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
static {
ParserConfig.getGlobalInstance().addAccept("com.xxx.xxx.bo");
ParserConfig.getGlobalInstance().addAccept("com.xxx.xxx.redis");
}
public GenericFastJson2JsonRedisSerializer() {
super();
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
FastJsonWraper<T> wraperSet = new FastJsonWraper<>(t);
return JSON.toJSONString(wraperSet, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String deserializeStr = new String(bytes, DEFAULT_CHARSET);
FastJsonWraper<T> wraperGet = JSON.parseObject(deserializeStr, FastJsonWraper.class);
return wraperGet.getValue();
}
}
添加:
static {
ParserConfig.getGlobalInstance().addAccept("com.xxx.xxx.bo");
ParserConfig.getGlobalInstance().addAccept("com.xxx.xxx.redis");
}
所需要添加的包名可以在redis里面获取的字符串里面打印一下
把@type的value值添加到ParserConfig的白名单里面即可
有错误的欢迎指正 哦!!!!看到会马上恢复
每天记录一点点。。。。即便自己是菜鸟