Jackson ObjectMapper无法反序列化全小写字符串字段问题求助
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
JaxbAnnotationModuleswitches 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)andwithSetterVisibility(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()andsetFingerabdruckreferenznummer()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

