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

博客讲述了在使用GenericJackson2JsonRedisSerializer时遇到的InvalidDefinitionException,通过重写ObjectMapper并配置相关特性来解决。然而,尽管解决了报错,反序列化后对象变为HashMap导致新的运行时异常。最终通过更新ObjectMapper的default typing配置成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用序列化类:GenericJackson2JsonRedisSerializer

错误信息:

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"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:160) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:730) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:657) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeTypedContents(IndexedListSerializer.java:181) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:92) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:18) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serializeWithType(AsArraySerializerBase.java:267) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4568) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(ObjectMapper.java:3844) ~[jackson-databind-2.13.3.jar!/:2.13.3]
	at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.serialize(GenericJackson2JsonRedisSerializer.java:123) ~[spring-data-redis-2.7.0.jar!/:2.7.0]
	... 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,然后覆盖默认的


    public ObjectMapper getObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
        mapper.registerModule(javaTimeModule);
        mapper.registerModule(new Jdk8Module());
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setTimeZone(Calendar.getInstance().getTimeZone());
//        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        return mapper;
    }

 public RedisTemplate<String, Object> redisTemplate(int db) {
        //为了开发方便,一般直接使用<String,Object>
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnection(db)); //设置连接

        template.setDefaultSerializer(new StringRedisSerializer());

        // 使用 GenericFastJsonRedisSerializer 替换默认序列化
        //这里覆盖默认的ObjectMapper
        GenericJackson2JsonRedisSerializer genericFastJsonRedisSerializer = new GenericJackson2JsonRedisSerializer(getObjectMapper());
        // 设置key和value的序列化规则
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(genericFastJsonRedisSerializer);
        // 设置hashKey和hashValue的序列化规则
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(genericFastJsonRedisSerializer);
        // 设置支持事物
//        template.setEnableTransactionSupport(true);

        template.afterPropertiesSet();
        return template;
    }

但是经过测时候,解决开篇的报错信息,但是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,设置 其属性。完全解决了所有问题,代码经过测试

//过期的方法
//        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

//替换方法
        mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

在Vue.js中创建SVG图标通常涉及到使用`vue-cli`脚手架命令行工具。如果你想通过`vue create`生成一个新的Vue项目并包含SVG图标的支持,可以按照以下步骤操作: 1. **安装Vue CLI**:首先确保已经全局安装了Vue CLI,如果没有,运行`npm install -g @vue/cli`。 2. **初始化项目**:打开终端或命令提示符,导航到你想创建项目的文件夹,然后输入`vue create project-name`(替换`project-name`为你的项目名称)。 3. **选择模板**:当脚手架询问你想要哪种预设时,选择“Manually select features”(手动选择特性),然后按Enter继续。 4. **添加自定义配置**:在接下来的配置界面,找到`Additional configuration options`部分,在这里添加对`svg-sprite-loader`的支持。在`Webpack chain`选项里,你可以看到一个编辑器,搜索`svg`,在`resolve.alias`下添加一个新条目,例如: ```json { "icons": path.resolve(__dirname, "./src/icons"), } ``` 5. **安装依赖**:在`Additional packages to install`部分,输入`@vue/babel-plugin-jsx` 和 `svg-sprite-loader`,然后点击`Create project`。 6. **创建SVG组件**:在`src/components`目录下创建一个名为`SvgIcon.vue`的新文件,内容如下: ```html <template> <use :href="iconPath" /> </template> <script setup> import { ref } from 'vue'; const iconPath = ref(`icons/${props.name}.svg`); </script> ``` 7. **使用SVG图标**:现在可以在其他组件中导入并使用SVG图标,例如: ```html <SvgIcon name="your-icon-name" /> ``` 完成以上步骤后,你就可以在Vue项目中创建、引用和管理SVG图标了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值