autoType is not support. org.springframework.cache.interceptor.SimpleKey

原创
2022/08/22 17:44
阅读数 22

redis使用序列化异常,具体信息如下:

com.alibaba.fastjson.JSONException: autoType is not support. org.springframework.cache.interceptor.SimpleKey
	at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1437)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:343)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1430)
	at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:46)
	at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_3_CacheMessage.deserialze(Unknown Source)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:287)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:405)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1430)
	at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:46)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:705)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:394)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:298)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:588)
	at com.iflytek.kids.area.common.redis.configure.FastJson2JsonRedisSerializer.deserialize(FastJson2JsonRedisSerializer.java:52)
	at com.iflytek.kids.area.common.redis.cache.support.CacheMessageListener.onMessage(CacheMessageListener.java:23)
	at org.springframework.data.redis.listener.RedisMessageListenerContainer.executeListener(RedisMessageListenerContainer.java:263)
	at org.springframework.data.redis.listener.RedisMessageListenerContainer.processMessage(RedisMessageListenerContainer.java:253)
	at org.springframework.data.redis.listener.RedisMessageListenerContainer.lambda$dispatchMessage$1(RedisMessageListenerContainer.java:1051)
	at java.base/java.lang.Thread.run(Thread.java:834)

序列化类重点代码:

[@Override](https://my.oschina.net/u/1162528)
    public byte[] serialize(T t) throws SerializationException {
        if (t == null) {
            return new byte[0];
        }
        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    [@Override](https://my.oschina.net/u/1162528)
    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);
        // 如果存在这个SimpleKey对应的数据,那么设置禁用关键字解析。
        if(str.indexOf("org.springframework.cache.interceptor.SimpleKey") > -1){
            return JSON.parseObject(str, clazz, Feature.DisableSpecialKeyDetect);
        }
        return JSON.parseObject(str, clazz);
    }

前后参考了好几篇: https://blog.csdn.net/ttyy1112/article/details/107052038/ https://www.pudn.com/news/6290332707732924f786e94d.html https://www.dazhuanlan.com/dada1171/topics/1012827

由于结合cacheable多级缓存使用,最后使用的解决方案是在反序列化方法添加:

        // 如果存在这个SimpleKey对应的数据,那么设置禁用关键字解析。
        if(str.indexOf("org.springframework.cache.interceptor.SimpleKey") > -1){
            return JSON.parseObject(str, clazz, Feature.DisableSpecialKeyDetect);
        }
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部