尝试使用 RestTemplate 获取 Objects 列表时出现空响应 body

Having empty response body when trying to get lists of Objects with RestTemplate

所以我正在开发一个 REST 客户端,该客户端使用 Spring RestTemplate 使用 REST API 获取 json objects 的列表。我正在使用 api 键设置必要的 headers。所以我收到 HTTP 200 OK 响应,但响应 body 为空。当我使用 Postman 执行相同的请求时,它运行良好。这可能是什么原因?

代码片段:

 public List<PoyntSubscription> getSubscriptions(String apiToken, String cloudId, String cloudBaseUrl) {
    List<PoyntSubscription> subscriptions = new ArrayList<>();

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Api-Version", "1.2");
    headers.set("Authorization", apiToken);
    HttpEntity entity = new HttpEntity(headers);

    ResponseEntity<PoyntSubscription> response = restTemplate.exchange(
            cloudBaseUrl + cloudId + "/subscriptions?start=10", HttpMethod.GET, entity, PoyntSubcriptionsList.class);
    return response.getBody().getSubscriptions();
}

当邮递员使用 API 时我得到的响应 json:

{
"list": [
    {
        "startAt": "2019-01-22T00:00:00Z",
        "paymentStatus": "OVERDUE",
        "createdAt": "2019-01-22T03:05:28Z",
        "updatedAt": "2019-02-21T03:05:28Z",
        "businessId": "xxxx",
        "appId": "xxxx",
        "subscriptionId": "xxxxx",
        "phase": "FULL",
        "planId": "xxxx",
        "bundleId": "xxxx",
        "planName": "xxxx",
        "status": "ACTIVE"
    }
 ],
 "start": 10,
 "total": 14,
 "count": 4
}

PoyntSubscription 包装器 class:

public class PoyntSubcriptionsList {
private List<PoyntSubscription> subscriptions = new ArrayList();

public PoyntSubcriptionsList() {
}

public List<PoyntSubscription> getSubscriptions() {
    return this.subscriptions;
}

public void setSubscriptions(List<PoyntSubscription> subscriptions) {
    this.subscriptions = subscriptions;
 }
}

Poynt订阅 class :

public class PoyntSubscription {
private String startedDate;
private String paymentStatus;
private String createdDate;
private String updatedDate;
private String businessId;
private String appId;
private String subscriptionId;
private String phase;
private String planId;
private String bundleId;
private String planName;
private String status;

public PoyntSubscription() {
}

在 class PoyntSubcriptionsList 中用 @JsonGetter("list") 注释 getSubscriptions()

您还需要将 ResponseEntity<PoyntSubscription> response 更改为 ResponseEntity<PoyntSubcriptionsList> response,因为 PoyntSubcriptionsList 代表您的 JSON。