简体   繁体   中英

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

So i'm working on a REST client that consumes a REST API to get list of json objects using the Spring RestTemplate. I'm setting the necessary headers with the api key. So i get a HTTP 200 OK response but the response body is empty. When I do the same request using the Postman it works well. What might be the reason for this?

The code snippet :

 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();
}

The response json i'm getting when the API is consumed by the postman:

{
"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 wrapper 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;
 }
}

PoyntSubscription 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() {
}

Annotate getSubscriptions() with @JsonGetter("list") in class PoyntSubcriptionsList .

You also need to change ResponseEntity<PoyntSubscription> response to ResponseEntity<PoyntSubcriptionsList> response as PoyntSubcriptionsList represents your JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM