解决com.alibaba.fastjson.JSONException: autoType is not support 异常处理

原文地址:https://blog.csdn.net/wgzhl2008/article/details/82184240

最近在使用spring-data-redis时,使用fastjson的序列化方式

GenericFastJsonRedisSerializer
可以正常序列化,但在反序列化时发生了如下异常com.alibaba.fastjson.JSONException: autoType is not support. com.hongshu.groovy.dto.Account。

 解决com.alibaba.fastjson.JSONException: autoType is not support 异常处理

仔细阅读了fastjson.ParserConfig源码,发现在1078行发生了异常

 解决com.alibaba.fastjson.JSONException: autoType is not support 异常处理

因为在GenericFastJsonRedisSerializer中,如下所示,把autoTypeSupport设置为true,

 解决com.alibaba.fastjson.JSONException: autoType is not support 异常处理

所以导致异常产生的原因是因为在fastjson在反序列化时,build JavaBeanInfo时。对是否有默认的构造方法做了检查。看到原因后,直接去自己的Domain中去查看,果然是没有默认的构造的方法。

总结:在使用fastjson类序列化时,尤其是redis这个序列的支持,在类中要加保证一默认的构造方法,否则在进行反序列化时,fastjosn根据typeName进行反序列化,会发生该异常