FastJSON2反序列化报错:Resolved [java.lang.ClassCastException: class com.alibaba.fastjson.JSONObject cannot
创始人
2025-01-10 07:05:10
0

在使用阿里FastJSON2将model类数据存储在redis过程中正常,从redis里取数据时报错,报错的完整信息如下:Resolved [java.lang.ClassCastException: class com.alibaba.fastjson.JSONObject cannot be cast to class com.example.demo.model.ProductPrice (com.alibaba.fastjson.JSONObject is in unnamed module of loader 'app'; com.example.demo.model.ProductPrice is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3305e9d3)]。

相关代码如下:public class FastJsonRedisSerializer implements RedisSerializer {

public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

private Class clazz;


public FastJsonRedisSerializer(Class clazz) {
super();
this.clazz = clazz;
}

//序列化
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}

//反序列化
@Override
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);
}

错误的原因是在存储进redis里时FastJSON会自动的填充Type类型,redis数据示例如下:{"@type":"com.example.demo.model.ProductPrice","attributevalueids":"10,12","picture":"cspImages1_1706161154346.jpg","price":28.80,"priceid":18,"productid":2,"sort":1,"stock":96}。但是在取数据反序列化过程中无法自动根据Type类型值转换为对应的model类,导致报错。

解决方案:

FASTJSON支持AutoType功能,这个功能会在序列化的JSON字符串中带上类型信息,在反序列化时,不需要传入类型,实现自动类型识别。

  • 必须显式打开才能使用。和fastjson 1.x不一样,fastjson 1.x为了兼容有一个白名单,在fastjson 2中,没有任何白名单,也不包括任何Exception类的白名单,必须显式打开才能使用。这可以保证缺省配置下是安全的。
  • 支持配置safeMode,在safeMode打开后,显式传入AutoType参数也不起作用。
  • 显式打开后,会经过内置黑名单过滤。该黑名单能拦截大部分常见风险,这个机制不能保证绝对安全,打开AutoType不应该在暴露在公网的场景下使用。
  • AutoType功能使用介绍

    3.1 序列化时带上类型信息

    如果需要序列化时带上类型信息,需要使用JSONWriter.Feature.WriteClassName。比如:

    Bean bean = ...; String jsonString = JSON.toJSONString(bean, JSONWriter.Feature.WriteClassName);

    很多时候,root对象是可以知道类型的,里面的对象字段是基类或者不确定类型,这个时候不输出root对象的类型信息,可以减少序列化结果的大小,也能提升反序列化的性能。

    Bean bean = ...; String jsonString = JSON.toJSONString(bean, JSONWriter.Feature.WriteClassName, JSONWriter.Feature.NotWriteRootClassName)

    3.2 反序列化时打开AutoType功能以支持自动类型

    Bean bean = (Bean) JSON.parseObject(jsonString, Object.class, JSONReader.Feature.SupportAutoType);。相关的完整代码如下:
  • import com.alibaba.fastjson2.JSON;
    import com.alibaba.fastjson2.JSONReader;
    import com.alibaba.fastjson2.JSONWriter;
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.type.TypeFactory;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.SerializationException;

    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;

    public class FastJsonRedisSerializer implements RedisSerializer {

    public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

    private Class clazz;


    public FastJsonRedisSerializer(Class clazz) {
    super();
    this.clazz = clazz;
    }

    //序列化
    @Override
    public byte[] serialize(T t) throws SerializationException {
    if (t == null) {
    return new byte[0];
    }
    return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    //反序列化
    @Override
    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, JSONReader.Feature.SupportAutoType);
    }注意导包时import com.alibaba.fastjson2.JSON;和import com.alibaba.fastjson.JSON;的区别。另一种解决方案见链接:fastjson2_autotype_cn · alibaba/fastjson2 Wiki · GitHub

相关内容

热门资讯

八分钟辅助!拱趴大菠萝万能挂图... 八分钟辅助!拱趴大菠萝万能挂图解,如何下载wpk透视版,妙招教程(有挂秘笈)1、打开软件启动之后找到...
第9分钟辅助!wepoker辅... 第9分钟辅助!wepoker辅助插件功能,hhpoker辅助软件是真的么,项目教程(有挂头条)该软件...
第五分钟辅助!wpk作弊最怕三... 第五分钟辅助!wpk作弊最怕三个东西,wepoker免费脚本,手册教程(有挂总结)1、下载好wpk作...
9分钟辅助!德普之星透视辅助,... 9分钟辅助!德普之星透视辅助,wepokerplus辅助,方式教程(有挂透明挂)1、用户打开应用后不...
1分钟辅助!wepoker透视... 1分钟辅助!wepoker透视挂底牌,hhpoker透视脚本视频,方案教程(今日头条)1、玩家可以在...
第三分钟辅助!如何下载德普之星... 第三分钟辅助!如何下载德普之星辅助软件,智星菠萝有挂吗,模块教程(有挂教程)如何下载德普之星辅助软件...
9分钟辅助!hhpoker作弊... 9分钟辅助!hhpoker作弊码,xpoker辅助工具,教程书教程(有挂分析)运xpoker辅助工具...
第4分钟辅助!pokemmo手... 第4分钟辅助!pokemmo手机脚本,werplan辅助软件,总结教程(真是有挂)1、首先打开pok...
9分钟辅助!德州真人透视脚本,... 9分钟辅助!德州真人透视脚本,pokemmo脚本辅助器下载,妙计教程(存在有挂)1、超多福利:超高返...
第五分钟辅助!德普之星怎么设置... 您好,德普之星怎么设置埋牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...