1
votes

i am i little lost in creating a mapping with jackson. My Json has the following structure

    {
  "d": {
    "__metadata": {
      "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/JobApplication(1463L)",
      "type": "SFOData.JobApplication"
    },
    "lastName": "K",
    "address": "123 Main Street",
    "cellPhone": "12345",
    "firstName": "Katrin",
    "city": "Anytown",
    "country": "United States",
    "custappattachment": {
      "results": [
        {
          "__metadata": {
            "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/Attachment(1188L)",
            "type": "SFOData.Attachment"
          },
          "fileExtension": "jpeg",
          "fileName": "hp-hero-img.jpeg",
          "fileContent": "/9j/4AA"
        },
        {
          "__metadata": {
            "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/Attachment(1189L)",
            "type": "SFOData.Attachment"
          },
          "fileExtension": "jpeg",
          "fileName": "hp-content-bkgd-img.jpeg",
          "fileContent": "/9j/4AAQSk"
        }]}}}

I do find a lot of tutorials handling arrays, but i fail already with the very first token "d". and all the "__metadata" token are not needed at all.

I created a pojo containing attributes like lastName etc. and a collection attachments. But my code always fails at token "d" or "__metadata"

public class ResponseDataObject {


    private String lastName;
    private String address;
    private String cellPhone;
    private String firstName;
    private String city;
    private String country;
    private List<Attachment> attachments = new ArrayList<>();
    .....getters and setters

and the jackson reader

    ObjectReader objectReader =
    mapper.readerFor(ResponseDataObject.class);
    ResponseDataObject dataObject = objectReader.readValue(file);

Any hints would be appreciated.

Regards Mathias

2

2 Answers

1
votes

You can use @JsonIgnoreProperties annotation to ignore the properties that don't match any of the members defined in class, e.g.:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseDataObject {

This will make sure all the matching properties are mapped and other properties are ignored.

0
votes

You need to ignore properties, not present in POJO. Set following property in DeserializationFeature for ObjectMapper:

// for version 1.x       
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// for newer versions
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) 

Deserialization code:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
ResponseDataObject dataObject = mapper.readValue(file, ResponseDataObject.class);

and add this annotation to ResponseDataObject class:

@JsonRootName(value = "d")
class ResponseDataObject {