I have a json as a string:
{
"passnummer": "passnummer",
"nummerDesAufenthaltstitels": "nummerDesAufenthaltstitels",
"ppkNummer": "ppkNummer",
"fingerabdruckreferenznummer": "fingerabdruckreferenznummer"
}
there is no consistency for the seperation of the words meaning that some keys are camel case and some are not. I have no influence on that. And I cannot change the model.
I have to read this string into a Java Object. I also have no influennce on the Java Model so I cannot add @JsonProperty or @JsonAlias annotations or anything else to the fields of the model.
So the java model looks like this:
public static class Request {
protected String passnummer;
protected String nummerDesAufenthaltstitels;
protected String ppkNummer;
protected String fingerabdruckreferenznummer;
public String getPassnummer() {
return this.passnummer;
}
public void setPassnummer(String value) {
this.passnummer = value;
}
public String getNummerDesAufenthaltstitels() {
return this.nummerDesAufenthaltstitels;
}
public void setNummerDesAufenthaltstitels(String value) {
this.nummerDesAufenthaltstitels = value;
}
public String getPpkNummer() {
return this.ppkNummer;
}
public void setPpkNummer(String value) {
this.ppkNummer = value;
}
public String getFingerabdruckreferenznummer() {
return this.fingerabdruckreferenznummer;
}
public void setFingerabdruckreferenznummer(String value) {
this.fingerabdruckreferenznummer = value;
}
}
When I deserialize the json into the java object and print the values:
Request request = objectmapper.readValue(jsonPayload, Request.class);
System.out.println("passnummer: " request.getPassnummer());
System.out.println("nummerDesAufenthaltstitels: " request.getNummerDesAufenthaltstitels());
System.out.println("ppkNummer: " request.getPpkNummer());
System.out.println("fingerabdruckreferenznummer: " request.getFingerabdruckreferenznummer());
I get the following result:
passnummer: passnummer
nummerDesAufenthaltstitels: nummerDesAufenthaltstitels
ppkNummer: ppkNummer
fingerabdruckreferenznummer: null
So my question is: Why cant objectMapper desieralize fingerabdruckreferenznummer
?
EDIT
ObjectMapper config:
public ObjectMapper objectMapper() {
final ObjectMapper objectMapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addSerializer(TeilbekanntesDatumMitUnbekannt.class, new DateSerializer());
objectMapper.registerModule(module);
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new JaxbAnnotationModule());
objectMapper.configure(WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
return objectMapper;
}
EDIT 2
If I use a play new ObjectMapper()
instead I get:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "fingerabdruckreferenznummer"
CodePudding user response:
Using your ObjectMapper
(except the parts that you didn't post) and this code, the result is as expected:
Code
final ObjectMapper objectMapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
// module.addSerializer(TeilbekanntesDatumMitUnbekannt.class, new DateSerializer());
// objectMapper.registerModule(new JaxbAnnotationModule());
objectMapper.registerModule(module);
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
final String jsonPayload = """
{
"passnummer": "1passnummer",
"nummerDesAufenthaltstitels": "2nummerDesAufenthaltstitels",
"ppkNummer": "3ppkNummer",
"fingerabdruckreferenznummer": "4fingerabdruckreferenznummer"
}
""";
final Request request = objectMapper.readValue(jsonPayload, Request.class);
System.out.println("passnummer: " request.getPassnummer());
System.out.println("nummerDesAufenthaltstitels: " request.getNummerDesAufenthaltstitels());
System.out.println("ppkNummer: " request.getPpkNummer());
System.out.println("fingerabdruckreferenznummer: " request.getFingerabdruckreferenznummer());
Result
passnummer: 1passnummer
nummerDesAufenthaltstitels: 2nummerDesAufenthaltstitels
ppkNummer: 3ppkNummer
fingerabdruckreferenznummer: 4fingerabdruckreferenznummer
So either your json-payload is different, or your objectmapper
has some additional config.
CodePudding user response:
The ObjectMapper
could be configured to filter out certain fields. Create you own ObjectMapper or make sure you know what is configured.
This could be done with a Mixin.
this.objectMapper = objectMapper.addMixIn(Request.class, MyMixin.class);
@JsonIgnoreProperties({"fingerabdruckreferenznummer", "field2"})
private interface MyMixin {}