Parsing JSON using Jackson: Can we have optional variables in a POJO

DevRight :

I have a POJO with the following variables.

private Long poId;
private String nodeName;
private String fingerPrint;
private String hardwareResourceName;
private String sequenceNumber;
private String importedOn;
private String importedBy;
private String generated;
private String installedOn;
private String currentLicense;
private String productType;

Which is used to parse details about license files? When we make a call to certain rest endpoints, we get the following JSON response.

[{"nodeName":"LTE02ERBS00042","fingerprint":"LTE02ERBS00042_fp","hardwareResourceName":"ELSN","generatedOn":"1251359414000","installedOn":"","currentLicense":"false","sequenceNumber":9000,"importedOn":"1531929161227","poId":281475302769484,"filePath":"/home/smrs/smrsroot/licence/LTE02ERBS00042_fp/LTE02ERBS00042_fp_151008_060941.xml","importBy":"agatAdm7160","neType":"ERBS","platformType":"CPP"}

As you can see from the JSON, there are mismatches from what is returned in the response to the POJO e.g.: from pojo: fingerPrint from JSON: fingerprint. Also the JSON has extra fields which are not in the POJO. The POJO is used in other area's, so I do not want to change the existing functionality. I would like to add extra fields to accommodate the change in the data being returned, without having to create a separate POJO.

Is there a way with annotations that I can set a variable in the POJO to be optional or not always needed? So in cases where we get a JSON response with all the variables great, but if the response only has certain data, it will work also.

Karol Dowbecki :

Jackson has various features for dealing with this e.g. FAIL_ON_UNKNOWN_PROPERTIES can be switched off to allow for unknown properties in JSON that don't exist in POJO:

new ObjectMapper()
    .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

Take a look at Jackson wiki which lists available features:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91338&siteId=1