所以我正在使用REST客户端的REST客户端上使用Spring RestTemplate来获取json对象的列表。我正在使用api键设置必要的标头。所以我得到一个HTTP 200 OK响应,但是响应主体为空。当我使用邮递员执行相同的请求时,效果很好。可能是什么原因?
代码段:
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包装器类:
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类:
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() {
}
答案 0 :(得分:1)
在类getSubscriptions()
中用@JsonGetter("list")
注释PoyntSubcriptionsList
。
您还需要将ResponseEntity<PoyntSubscription> response
更改为ResponseEntity<PoyntSubcriptionsList> response
,因为PoyntSubcriptionsList
代表您的JSON。