使用RestTemplate对REST API调用中的对象列表具有空响应

时间:2019-03-13 07:02:29

标签: java spring resttemplate jsonschema2pojo

因此,我正在使用REST客户端,该客户端使用REST API来使用Spring RestTemplate获取JSON对象。所以我得到一个HTTP 200 OK响应,但是类对象内的列表(设备)为空。但是其他领域也被获取。当我使用邮递员执行相同的请求时,效果很好。可能是什么原因?

RestTemplate代码段:

 RestTemplate restTemplate = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Content-Type", "application/json");
requestHeaders.add("Authorization", "Bearer " + apiToken);
HttpEntity entity = new HttpEntity(requestHeaders);
ResponseEntity<CloverMerchant> response = restTemplate.exchange(getMerchantDetailsUrl, HttpMethod.GET, entity, CloverMerchant.class);
return response.getBody();

CloverMerchant模型类:

private String id;
private String name;
private String website;
private boolean isBillable;
private CloverBusinessEquipments equipment;

// other relevant getters and setters

CloverBusinessEquipments模型类:

private List<CloverBusinessEquipment> elements;

    public CloverBusinessEquipments() {
}

@JsonGetter("elements")
public List<CloverBusinessEquipment> getElements() {
    return elements;
}

CloverBusinessEquipment模型类:

private String merchantId;
private String serialNumber;
private String equipmentCode;
private String equipmentCodeDesc;
private String provisionedDeviceType;
private boolean boarded;
private boolean provisioned;

// relevant getters and setters

REST API的预期响应JSON:

{
  "id": "5ZTFCGXQKVZNA",
  "name": "xxxx",
  "website": "https://xxxx.io",
  "isBillable": false,
  "equipment": {
  "elements": [
    {
        "merchantId": "5ZTFCGXQKVZNA",
        "boarded": false,
        "provisioned": true,
        "serialNumber": "C030UQ71040182",
        "equipmentCode": "105J",
        "equipmentCodeDesc": "Clover Mini",
        "provisionedDeviceType": "MAPLECUTTER"
    },
    {
        "merchantId": "5ZTFCGXQKVZNA",
        "boarded": false,
        "provisioned": true,
        "serialNumber": "C050UQ75150054",
        "equipmentCode": "1297",
        "equipmentCodeDesc": "Clover Station 2018",
        "provisionedDeviceType": "GOLDENOAK"
       }
    ]
  }
}

2 个答案:

答案 0 :(得分:0)

您的模型不代表JSON响应。您正在尝试在JSON的根级别上找到键为"elements"的JSONObject,但实际上它位于键为"equipment"之后的第二级。

变量-private CloverBusinessEquipments equipments;应表示以下内容:

"equipment": {
    "elements": [
        {
            "merchantId": "5ZTFCGXQKVZNA",
            "boarded": false,
            "provisioned": true,
            "serialNumber": "C030UQ71040182",
            "equipmentCode": "105J",
            "equipmentCodeDesc": "Clover Mini",
            "provisionedDeviceType": "MAPLECUTTER"
        },
        {
            "merchantId": "5ZTFCGXQKVZNA",
            "boarded": false,
            "provisioned": true,
            "serialNumber": "C050UQ75150054",
            "equipmentCode": "1297",
            "equipmentCodeDesc": "Clover Station 2018",
            "provisionedDeviceType": "GOLDENOAK"
        }
    ]
}

但是您已经建立了POJO的模型,该模型认为equipments变量将如下所示:

"elements": 
        {
            "merchantId": "5ZTFCGXQKVZNA",
            "boarded": false,
            "provisioned": true,
            "serialNumber": "C030UQ71040182",
            "equipmentCode": "105J",
            "equipmentCodeDesc": "Clover Mini",
            "provisionedDeviceType": "MAPLECUTTER"
        }

答案 1 :(得分:0)

您需要按如下所示重新设计您的课程并检查

删除吸气剂方法
CloverMerchant.java

.each

使用以下代码更新CloverBusinessEquipments。
CloverBusinessEquipments.java

cy.get('img').each($el => {
  cy.wrap($el).should('have.attr', 'alt')
}

创建新的PoJo类
CloverBusinessEquipment.java

private String id;
private String name;
private String website;
@JsonProperty("isBillable")
private boolean isBillable;
private CloverBusinessEquipments equipment;

Main.java

private List<CloverBusinessEquipment> elements;

如果您可以检查上述主要方法,则可以从问题中发布的Json String进行反序列化。
我正在使用Jackson 2.9.8