Java 在SpringBoot中使用RESTAPI时如何检索所有页面
我使用以下代码在Spring Boot中使用GET调用,并将结果存储为JSON。但是,我只能检索100条记录,这是API的默认页面大小。如何检索所有页面的全部数据Java 在SpringBoot中使用RESTAPI时如何检索所有页面,java,spring,rest,http,spring-boot,Java,Spring,Rest,Http,Spring Boot,我使用以下代码在Spring Boot中使用GET调用,并将结果存储为JSON。但是,我只能检索100条记录,这是API的默认页面大小。如何检索所有页面的全部数据 public static void getData(String baseDir, String baseURI, String headerFieldName, String headerFieldValue) { RestTemplate restTem
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();
}
}
有没有办法只取计数字段?然后我可以使用它循环并从数据中获取所有记录
谢谢 我想我找到了一个解决办法,使用JsonPath获取计数
final String data = restTemplate.exchange(baseURI + api + "?limit=0", HttpMethod.GET, entity, String.class).getBody();
final int count = JsonPath.read(data, "$.count");
谢谢大家 在将字符串数据写入dataWriter之前,是否可以尝试打印该字符串数据。。。?另外,请共享方法dataWriter…通过浏览器发送请求时,您得到多少值?另外,尝试添加查询参数page=1,2,3。。。对于URIQuick更新:默认情况下,当我通过浏览器发送时,我会得到100个值,但是@MaruthiAdithya当我发送限制设置为count的请求时,我会得到所有记录。我现在已经更正了问题,例如/记录?限制=18000@MaruthiAdithya:他如何知道这个数字将来是否会改变?@NicholasK用dataWriter更新了这个问题
final String data = restTemplate.exchange(baseURI + api + "?limit=0", HttpMethod.GET, entity, String.class).getBody();
final int count = JsonPath.read(data, "$.count");