Home > OS >  how to split different json properties not wrapped in json object into multiple model java classes
how to split different json properties not wrapped in json object into multiple model java classes

Time:12-08

I have a big payload with different key property values. I tried creating model separate model classes based on the object on the portal. But there is no separate json object wrapped for such properties. My question is how to split the big json object into separate java classes.

Payload: { "documentType": "BDFFIIJCDSAAA", "documentGroup": "394EIIJCDSAAA", "ply": "0", "numberOfCopies": "1", "tpDocNumbers": [ "507317219" ], "product": { "vessel": null , "voyage": null , "countryOfReceipt": null, "includeCountryOfReceipt": "true", "countryOfLoading": null, "includeCountryOfLoading": "true", "countryOfDischarge": null, "includeCountryOfDischarge": "true", "iountryOfDelivery": null, "includeCountryOfDelivery": "true", "tradeLane": null, "includeTradeLane": "true", "placeOfReceipt": null , "includePlaceOfReceipt": "true", "loadPort": null, "includeLoadPort": "true", "dischargePort": null, "includeDischargePort": "true", "placeOfDelivery": null, "includePlaceOfDelivery": "true" }, "isTaskStatusBlank": "true", "isTaskStatusOpen": "true", "isTaskStatusClosed": "true","recepient": "Direct", "recepientPort": null, "communication": "Email", "contact": "[email protected]", "subject": "latest123", "docBrokerDomain": "", "printerName": "", "templateDomain": "", "priority": "", "freeText": "" }

Java Model classes - public class A {

@JsonProperty("DocumentType")
private String documentType;

@JsonProperty("DocumentGroup")
private String documentGroup;

private Ply ply;

@JsonProperty("PartyRoles")
private String partyRoles;

@JsonProperty("CustomerId")
private String customerId;

@JsonProperty("PartyRoleTypeId")
private String partyRoleTypeId;

@JsonProperty("PartyRefNo")
private String partyRefNo;

@JsonProperty("TPDocNumbers")
private List<String> tpDocNumbers;

@JsonProperty("Product")
private Product product;

private Cargo cargoInfo;

private TaskCriteria taskCriteria;

private Recipient recipientInfo;

@JsonProperty("DocBrokerDomain")
private String docBrokerDomain;

@JsonProperty("PrinterName")
private String printerName;

@JsonProperty("TemplateDomain")
private String templateDomain;

@JsonProperty("Priority")
private String priority;

@JsonProperty("RequestType")
private String requestType;

@JsonProperty("FreeText")
private String freeText;

}

public class Ply { @JsonProperty("Ply") private int plyNumber;// plySelection @JsonProperty("NumberOfCopies") private int copies;// copySelection @JsonProperty("freightSelection") private String freightSelection; }

This doesnt work and I get the error - "no String-argument constructor/factory method to deserialize from String value ('0')\n at" when it encounters the Ply Object class.

CodePudding user response:

Since in your JSON you have Ply properties in the same level as all the other A class properties, you need to use @JsonUnwrapped in the private Ply ply; property. Additionally, given that Jackson ObjectMapper is case sensitive while deserializing you also need to adjust the @JsonProperty annotations (if the property name match JSON property you don't even need the annotation):

public class A {
    private String documentType;

    private String documentGroup;

    @JsonUnwrapped
    private Ply ply;

    private String partyRoles;

    private String customerId;

    private String partyRoleTypeId;

    private String partyRefNo;

    private List<String> tpDocNumbers;

    private Product product;

    private Cargo cargoInfo;

    private TaskCriteria taskCriteria;

    private Recipient recipientInfo;

    private String docBrokerDomain;

    private String printerName;

    private String templateDomain;

    private String priority;

    private String requestType;

    private String freeText;
}

You also need to adjust Ply to match JSON properties:

public class Ply {
    @JsonProperty("ply")
    private int plyNumber;

    @JsonProperty("numberOfCopies")
    private int copies;

    private String freightSelection;
}

CodePudding user response:

The problem you are facing is because your JSON has ply as a string value and your POJO is using Ply as an Object of Ply class. Solution:

  1. change your POJO from Ply object to ply integer value.
  2. change your Json from ply string to Object of Ply with Ply number as a property value.
  3. You can also use ObjectMapper readTree to read JSON as a tree and create your mapping
  • Related