解決使用Redis 配置替換fastjson 反序列化報錯 com.alibaba.fastjson.JSONException: autoType is not support

這幾天用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的白名單裏面即可

 

有錯誤的歡迎指正 哦!!!!看到會馬上恢復

每天記錄一點點。。。。即便自己是菜鳥

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章