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

Jackson ObjectMapper无法反序列化全小写字符串字段问题求助

Why fingerabdruckreferenznummer isn't deserializing correctly

Let's break down the issues with both your custom ObjectMapper and the default one:

1. Custom ObjectMapper: Null value for fingerabdruckreferenznummer

The root cause here is the combination of JaxbAnnotationModule and your visibility configuration. Here's what's happening:

  • You’ve set fieldVisibility(JsonAutoDetect.Visibility.ANY) to let Jackson access protected fields directly.
  • But registering JaxbAnnotationModule switches Jackson to using JAXB’s property resolution rules, which prioritize getter/setter methods over raw field access (even without explicit JAXB annotations on your model).
  • Your visibility settings disable access to getters and setters (withGetterVisibility(NONE) and withSetterVisibility(NONE)). This creates a conflict: the JAXB module tries to resolve the property via the getter/setter, but Jackson can’t access those methods. At the same time, the module overrides the raw field access you enabled, leaving no way to map the JSON key to the field.

Why the other fields work?

For fields like passnummer or ppkNummer, the field name exactly matches the property name derived from the getter/setter. Even with the module, Jackson might fall back to field access for these cases (inconsistent behavior, but possible). However, for fingerabdruckreferenznummer, this fallback doesn’t trigger—likely due to the length of the name or an edge case in the JAXB module’s property resolution logic.

2. Default ObjectMapper: Unrecognized property exception

The default ObjectMapper throws this error because:

  • By default, Jackson only considers public fields for direct access. Your fields are protected, so they’re invisible to the default configuration.
  • Jackson then relies on getter/setter methods to identify properties. While your getFingerabdruckreferenznummer() and setFingerabdruckreferenznummer() methods follow JavaBean conventions, there might be an edge case with the all-lowercase field name causing Jackson to misresolve the property name (unexpected, but plausible). Alternatively, the default mapper’s strictness around unrecognized properties (enabled by default) flags the JSON key because it can’t map it to a visible property.

Fixes to try

Option 1: Remove JaxbAnnotationModule (if possible)

If you don’t need JAXB annotation support for other parts of your code, removing this module will let your field visibility settings work as intended. Jackson will directly map JSON keys to your protected fields, including fingerabdruckreferenznummer.

Option 2: Adjust visibility settings to allow getter/setter access

If you need to keep JaxbAnnotationModule, modify your visibility checker to allow getter/setter access while retaining field access:

objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
        .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
        .withGetterVisibility(JsonAutoDetect.Visibility.ANY)
        .withSetterVisibility(JsonAutoDetect.Visibility.ANY)
        .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

This lets the JAXB module resolve properties via getters/setters while still allowing direct field access.

Option 3: Override the naming strategy

Force Jackson to use field names directly by setting a strategy that ignores getter/setter-derived names:

objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.IDENTITY);

This maps JSON keys exactly to field names, regardless of getter/setter conventions.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 16:52:49