You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jackson Databind 2.12.7升级至2.13.5反序列化问题求助

问题解决方向

核心原因

Jackson 2.13.x 对enableDefaultTyping的多态类型解析逻辑做了调整:当配置DefaultTyping.NON_FINAL时,所有非final类(包括你明确指定的PlatformUser)都会被强制要求包含@class类型ID属性,而2.12.x版本中,若已明确指定反序列化目标类型,会跳过类型ID检查,这就是升级后报错的根本原因。

具体解决方案

1. 替换过时的enableDefaultTyping方法

enableDefaultTyping在Jackson 2.10+已被标记为过时,推荐使用更安全的activateDefaultTyping方法,搭配PolymorphicTypeValidator精准控制类型解析范围,避免对不需要多态的类强制要求类型ID:

private ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    // 替换enableDefaultTyping为activateDefaultTyping
    mapper.activateDefaultTyping(
        PolymorphicTypeValidator.instance(),
        ObjectMapper.DefaultTyping.NON_FINAL,
        JsonTypeInfo.As.PROPERTY
    );
    // 后续模块注册、配置保留(注意移除重复注册的模块)
    mapper.registerModule(new CoreJackson2Module());
    mapper.registerModule(new WebJackson2Module());
    mapper.registerModule(new WebServletJackson2Module());
    mapper.registerModule(new WebServerJackson2Module());
    mapper.registerModule(new Saml2Jackson2Module());
    // 移除重复的模块注册行
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.registerModule(new JavaTimeModule());
    mapper.addMixIn(PlatformUser.class, PlatformUserMixin.class);
    mapper.addMixIn(MapSession.class, MapSessionMixin.class);
    return mapper;
}

2. 单独排除PlatformUser的类型ID检查

如果必须保留原有enableDefaultTyping配置,可在PlatformUserMixin中添加注解,告诉Jackson该类不需要多态类型标识:

@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
public abstract class PlatformUserMixin {
    // 原有的userProperties排除配置,比如:
    @JsonIgnore
    abstract Map<String, String> getUserProperties();
}

若允许修改PlatformUser类,也可直接在类上添加@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)。

3. 修复重复注册的模块

你的配置中重复注册了Saml2Jackson2Module,重复注册可能导致配置冲突,建议删除其中一行重复代码:

// 保留一行即可,删除重复的注册语句
mapper.registerModule(new Saml2Jackson2Module());

4. 验证Mixin的反序列化排除逻辑

确保PlatformUserMixin对userProperties的排除配置正确(比如使用@JsonIgnore或自定义反序列化器),避免Mixin的配置干扰类型解析流程。


内容的提问来源于stack exchange,提问作者martin samm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 23:13:16