我正在努力从我的POJO中构造正确的->对象。抱歉,这有点冗长,我只是在学习Java工具。我需要以下结构:
{
"firstName":"John",
"lastName":"Doe",
"addressLine1":"1 High Street",
"addressLine2":"",
"addressLine3":"",
"addressLine4":"Buffalo",
"zipCode":"22313",
"customerEntitlements":[
{
"entitlementCode": "6",
"entitlementType": "G",
"validFrom": "1980-04-22",
"validTo": "2026-11-17",
"customerRestrictions": []
},
{
"entitlementCode": "4",
"entitlementType": "L",
"validFrom": "1980-04-22",
"validTo": "2026-11-17",
"customerRestrictions": []
}
],
"customerPresent":null,
"customerConsentFormSighted":null,
"customerCheckConsent":true,
"dob":"1982-01-01",
"customerEndorsements":[]
}我的POJO们现在这样:
Customers
@Data
public class Customers {
@JsonProperty("firstName")
private String firstName;
@JsonProperty("lastName")
private String lastName;
@JsonProperty("addressLine1")
private String addressLine1;
@JsonProperty("addressLine2")
private String addressLine2;
@JsonProperty("addressLine3")
private String addressLine3;
@JsonProperty("addressLine4")
private String addressLine4;
@JsonProperty("zipcode")
private String zipcode;
@JsonProperty("customerPresent")
private String customerPresent;
@JsonProperty("customerConsentFormSighted")
private String customerConsentFormSighted;
@JsonProperty("customerCheckConsent")
private Boolean customerCheckConsent;
@JsonProperty("dob")
private String dob;
@JsonProperty("customerEndorsements")
private List<Object> customerEndorsements = null;
@JsonProperty("customerEntitlements")
private List<CustomerEntitlements> customerEntitlements;
}CustomerEntitlements
@Data
public class CustomerEntitlements {
@JsonProperty("entitlementCode")
private String entitlementCode;
@JsonProperty("entitlementType")
private String entitlementType;
@JsonProperty("validFrom")
private String validFrom;
@JsonProperty("validTo")
private String validTo;
@JsonProperty("customerRestrictions")
private List<Object> customerRestrictions = null;
}添加准备发送的有效负载的代码如下:
c = new Customers();
ce1 = new CustomerEntitlements();
ce2 = new CustomerEntitlements();
ce1.setEntitlementCode("B");
ce1.setEntitlementType("F");
ce1.setValidFrom("1980-04-22");
ce1.setValidTo("2026-11-17");
ce1.setCustomerrRestrictions(new JSONArray());
ce2.setEntitlementCode("D");
ce2.setEntitlementType("F");
ce2.setValidFrom("1980-04-22");
ce2.setValidTo("2026-11-17");
ce2.setCustomerrRestrictions(new JSONArray());
c.setFirstName("John);
c.setLastName("Doe");
c.setAddressLine1("1 High Street);
c.setAddressLine2("");
c.setAddressLine3("");
c.setAddressLine4("Buffalo");
c.setZipcode("22313");
c.setCustomerEntitlements(Arrays.asList(ce1, ce2);
c.setCustomerPresent(null);
c.setCustomerConsentFormSighted(null);
c.setCustomerCheckConsent(true);
c.setDob("1982-01-01");
c.setCustomerEndorsements(new JSONArray());代码中我的(格式错误的) JSON输出:
{
"firstName":"John",
"lastName":"Doe",
"addressLine1":"1 High Street",
"addressLine2":"",
"addressLine3":"",
"addressLine4":"Buffalo",
"zipcode":"22313",
"customerPresent":null,
"customerConsentFormSighted":null,
"dob":"1956-01-01",
"customerEndorsements":[
],
"customerEntitlements":[
{
"entitlementCode": "6",
"entitlementType": "G",
"validFrom": "1980-04-22",
"validTo": "2026-11-17",
"customerRestrictions": []
},
{
"entitlementCode": "4",
"entitlementType": "L",
"validFrom": "1980-04-22",
"validTo": "2026-11-17",
"customerRestrictions": []
}
]
}发布于 2020-07-06 17:57:16
我能看到的唯一不同之处在于customerEntitlements。我相信你的二传手应该是这样的,
public void customerEntitlements(List<CustomerEntitlements> customerEntitlements)当你这么做的时候
c.setCustomerEntitlements(Collections.singletonList(ce1));
c.setCustomerEntitlements(Collections.singletonList(ce2));您可以看到ce2覆盖了ce1,相反,您必须这样做,
c.setCustomerEntitlements(Arrays.asList(ce1, ce2));希望能有所帮助..。
注意:如果排序也是一个问题,我相信您应该知道您的Pojo->Json排序
也许你可以尝试重新排序,
@JsonProperty("firstName")
private String firstName;
@JsonProperty("lastName")
private String lastName;
@JsonProperty("addressLine1")
private String addressLine1;
@JsonProperty("addressLine2")
private String addressLine2;
@JsonProperty("addressLine3")
private String addressLine3;
@JsonProperty("addressLine4")
private String addressLine4;
@JsonProperty("zipcode")
private String zipcode;
@JsonProperty("customerEntitlements")
private List<CustomerEntitlements> customerEntitlements;
@JsonProperty("customerPresent")
private String customerPresent;
@JsonProperty("customerConsentFormSighted")
private String customerConsentFormSighted;
@JsonProperty("customerCheckConsent")
private Boolean customerCheckConsent;
@JsonProperty("dob")
private String dob;
@JsonProperty("customerEndorsements")
private List<Object> customerEndorsements = null;https://stackoverflow.com/questions/62753266
复制相似问题