gpt4 book ai didi

java - 使用 RestTemplate 对 REST API 调用中的对象列表做出空响应

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:21 25 4
gpt4 key购买 nike

所以我正在开发一个 REST 客户端,它使用 REST API 来使用 Spring RestTemplate 获取 JSON 对象。因此,我收到 HTTP 200 OK 响应,但类对象内的列表(设备)为空。但会获取其他字段。当我使用 postman 执行相同的请求时,效果很好。这可能是什么原因?

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"
}
]
}
}

最佳答案

您的模型不代表 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"
}

关于java - 使用 RestTemplate 对 REST API 调用中的对象列表做出空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55136143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com