【发布时间】:2019-02-21 14:52:46
【问题描述】:
我正在使用以下代码在 Spring Boot 中使用 GET 调用,并将结果存储为 JSON。但是,我只能检索 100 条记录,这是 API 的默认页面大小。如何检索整个数据(所有页面)?
public static void getData(String baseDir, String baseURI, String headerFieldName, String headerFieldValue) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add(headerFieldName, headerFieldValue);
HttpEntity<String> entity = new HttpEntity<String>(headers);
String data = restTemplate.exchange(baseURI + HttpMethod.GET, entity, String.class).getBody();
dataWriter(data, baseDir + File.separator + File.separator + ".json");
}
private static void dataWriter(final String data, final String filePath)
{
try {
Files.write(Paths.get(filePath), data.getBytes());
}
catch (final IOException e) {
e.printStackTrace();
}
}
地点:
baseURI = www.xxxxxx.com/records
headerFieldName 设置为“Authorization”,并且 headerFieldValue 包含访问令牌。
数据集包含大约 18000 条记录。
调用 API 后,我得到以下响应,其中 count 是响应中的记录数。
{
"count": 18000,
"data": [{
"id": "96d8-3024739ed555",
.....
.....
},
{
"id": "8595-0a27472aef0e",
.....
.....
}
...
....
]
}
有没有办法只获取“计数”字段?然后我可以用它来循环并从“数据”中获取所有记录。
谢谢!
【问题讨论】:
-
您可以在将字符串
data写入dataWriter(...)之前尝试打印它吗?也请分享方法dataWriter(...) -
通过浏览器发送请求时得到多少个值?另外,尝试将查询参数
page=1,2,3...添加到 URI -
快速更新:默认情况下,当我通过浏览器发送它时,我得到 100 个值,但是@MaruthiAdithya 当我发送限制设置为“计数”的请求时,我得到了所有记录。我现在已经纠正了这个问题,例如/records?limit=18000
-
@MaruthiAdithya:他怎么知道这个数字将来是否会发生变化?
-
@NicholasK 用 dataWriter 更新了问题
标签: java spring rest http spring-boot