• springboot集成redis,使用jackson序列化方案报Type id handling not implemented for 错误问题处理


    使用序列化类:GenericJackson2JsonRedisSerializer

    错误信息:

    1. Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Type id handling not implemented for type java.lang.Object (by serializer of type com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer) (through reference chain: java.util.ArrayList[0]->xxx.xx.xx.xx["createTime"])
    2. at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    3. at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    4. at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    5. at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:160) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    6. at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:730) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    7. at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    8. at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:657) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    9. at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeTypedContents(IndexedListSerializer.java:181) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    10. at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:92) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    11. at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:18) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    12. at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serializeWithType(AsArraySerializerBase.java:267) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    13. at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    14. at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    15. at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    16. at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4568) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    17. at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(ObjectMapper.java:3844) ~[jackson-databind-2.13.3.jar!/:2.13.3]
    18. at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.serialize(GenericJackson2JsonRedisSerializer.java:123) ~[spring-data-redis-2.7.0.jar!/:2.7.0]
    19. ... 30 more

     参考:​​​​​​https://blog.csdn.net/Ellen_Tangxiang/article/details/111310153

    参考:json - Jackson serialization failing when upgrading from 2.10 (InvalidDefinitionException: Type id handling not implemented for type java.lang.Object) - Stack Overflow

    在参考其他的一些博客后

    给到些想法,可以重新定义下 ObjectMapper 对象,使其兼容不可序列化的类型

    重写objectMap,然后覆盖默认的

    1. public ObjectMapper getObjectMapper() {
    2. ObjectMapper mapper = new ObjectMapper();
    3. mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    4. mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    5. mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    6. mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    7. JavaTimeModule javaTimeModule = new JavaTimeModule();
    8. javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
    9. mapper.registerModule(javaTimeModule);
    10. mapper.registerModule(new Jdk8Module());
    11. mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    12. mapper.setTimeZone(Calendar.getInstance().getTimeZone());
    13. // mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    14. mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    15. return mapper;
    16. }
    17. public RedisTemplate<String, Object> redisTemplate(int db) {
    18. //为了开发方便,一般直接使用<String,Object>
    19. RedisTemplate<String, Object> template = new RedisTemplate<>();
    20. template.setConnectionFactory(redisConnection(db)); //设置连接
    21. template.setDefaultSerializer(new StringRedisSerializer());
    22. // 使用 GenericFastJsonRedisSerializer 替换默认序列化
    23. //这里覆盖默认的ObjectMapper
    24. GenericJackson2JsonRedisSerializer genericFastJsonRedisSerializer = new GenericJackson2JsonRedisSerializer(getObjectMapper());
    25. // 设置keyvalue的序列化规则
    26. template.setKeySerializer(new StringRedisSerializer());
    27. template.setValueSerializer(genericFastJsonRedisSerializer);
    28. // 设置hashKey和hashValue的序列化规则
    29. template.setHashKeySerializer(new StringRedisSerializer());
    30. template.setHashValueSerializer(genericFastJsonRedisSerializer);
    31. // 设置支持事物
    32. // template.setEnableTransactionSupport(true);
    33. template.afterPropertiesSet();
    34. return template;
    35. }

    但是经过测时候,解决开篇的报错信息,但是Jackson序列化对象,保存在reids时。保存的完整json数据格式,存入redis的对象信息反序列化后,全部变成了 hashmap接口,导致业务处理报错

    java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.xx.xx.xx

    j经过查询后 ObjectMapper没有配置DefaultTyping属性,jackson将使用简单的数据绑定具体的java类型,其中Object就会在反序列化的时候变成LinkedHashMap......

    再回过头来看下xml中的json序列化实现类

    代码编写时发现方法已过期,找到了替代的方法,创建object Mapper,设置 其属性。完全解决了所有问题,代码经过测试

    1. //过期的方法
    2. // mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    3. //替换方法
    4. mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

  • 相关阅读:
    Linux交换分区要点汇总
    鉴源论坛丨信号基础设备概述
    设置单元格格式无效,Excel中删除某列的千分位符号
    CVE-2023-23752:Joomla未授权访问漏洞复现
    10 个 PHP 代码安全漏洞扫描程序
    高校校园网规划与设计
    2023年Q3企业邮箱安全性报告:境内钓鱼邮件超过境外攻击
    如何画产品架构图?
    编译器优化等级对程序性能的影响
    Windows编写批处理脚本.bat启动jar文件
  • 原文地址:https://blog.csdn.net/yuhaiyang_1/article/details/124251806