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

相关内容

热门资讯

技巧辅助挂!pokermast... 技巧辅助挂!pokermaster修改器,丹东约战麻将辅助器,演示教程(有挂细节)1、点击下载安装,...
现场直击!wepokerplu... 现场直击!wepokerplus万能挂,丰城双剑新版最强高分攻略,操作教程(有挂方针)1.丰城双剑新...
插件辅助挂!wepoker有辅... 插件辅助挂!wepoker有辅助器吗,乐平包王攻略,学习教程(有挂方略)1、首先打开乐平包王攻略辅助...
据玩家消息!拱趴大菠萝辅助神器... 据玩家消息!拱趴大菠萝辅助神器,多乐跑得快辅助器,机巧教程(证实有挂)1、在拱趴大菠萝辅助神器插件功...
此事备受玩家关注!来玩app破... 此事备受玩家关注!来玩app破解版,h5能反杀吗,绝活教程(有挂详细)1、打开软件启动之后找到中间准...
值得注意的是!aapoker破... 值得注意的是!aapoker破解侠是真的吗,蜀山四川游戏修改工具,经验教程(有挂助手)1、金币登录送...
第三方辅助!wepoker脚本... 第三方辅助!wepoker脚本,广东星悦有外开挂辅助器吗,法门教程(有挂分析)广东星悦有外开挂辅助器...
此事引发广泛关注!德州透视脚本... 此事引发广泛关注!德州透视脚本,崇阳斗棋辅助脚本视频,诀窍教程(的确有挂)暗藏猫腻,小编详细说明崇阳...
黑科技辅助挂!wepoker买... 黑科技辅助挂!wepoker买脚本靠谱吗,情怀七喜游戏辅助,法门教程(有挂方法)1、每一步都需要思考...
方法辅助挂!aapoker怎么... 方法辅助挂!aapoker怎么设置提高好牌几率,蘑菇云辅助使用视频,绝活儿教程(讲解有挂)1、完成蘑...