我构建了一个分页的API,其输出如下所示:
{
"content": [
{JSON1},
{JSON2},
...
{JSON20}
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"offset": 0,
"pageSize": 20,
"pageNumber": 0,
"unpaged": false,
"paged": true
},
"totalPages": 2,
"totalElements": 32,
"last": false,
"size": 20,
"number": 0,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"numberOfElements": 20,
"first": true,
"empty": false
}
因此,对于此调用,我有两页,每页有20个JSON实体即将出现。
我想从其余模板中调用相同的端点。
在分页之前,我曾经这样称呼过相同的端点:
MyEntity[] responseEntity;
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
// request entity is created with request headers
HttpEntity<MyEntity> requestEntity = new HttpEntity<>(requestHeaders);
Map<String, String> params = new HashMap<>();
params.put("feild1", val1);
params.put("feild2", val2);
responseEntity = restTemplate.getForObject(ApiEndpoint,MyEntity[].class,params);
当端点以MyEntity Array的格式返回时,上述代码就足够了。现在我已经对休息端点进行了分页。
如何调用分页端点并再次获取MyEntity数据数组?
到目前为止,我已经尝试通过以下方式致电:这对我不起作用。
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ApiEndpoint);
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue());
}
ResponseEntity<MyEntity[]> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, requestEntity, MyEntity[].class);
让我知道您是否可以在这里实现其他方式。感谢您的帮助。
答案 0 :(得分:0)
我所做的是,创建了新类RestPageImpl
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestPageImpl<ConfigurationTable> extends PageImpl<ConfigurationTable> {
private static final long serialVersionUID = -1423116752405536063L;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestPageImpl(
@JsonProperty("content") List<ConfigurationTable> content,
@JsonProperty("number") int number, @JsonProperty("size") int size,
@JsonProperty("totalElements") Long totalElements, @JsonProperty("pageable") JsonNode pageable,
@JsonProperty("last") boolean last, @JsonProperty("totalPages") int totalPages,
@JsonProperty("sort") JsonNode sort, @JsonProperty("first") boolean first,
@JsonProperty("numberOfElements") int numberOfElements) {
super(content, PageRequest.of(number, size), totalElements);
}
public RestPageImpl(List<ConfigurationTable> content, Pageable pageable,
long total) {
super(content, pageable, total);
}
public RestPageImpl(List<ConfigurationTable> content) {
super(content);
}
public RestPageImpl() {
super(new ArrayList<>());
}
}
在控制器端,我的代码更新为:
MyEntity[] responseEntity =null;
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
// request entity is created with request headers
HttpEntity<MyEntity> requestEntity = new HttpEntity<>(requestHeaders);
Map<String, String> params = new HashMap<>();
params.put("feild1", val1);
params.put("feild2", val2);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ApiEndpoint);
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue());
}
ParameterizedTypeReference<RestPageImpl<MyEntity>> type = new ParameterizedTypeReference<RestPageImpl<MyEntity>>() {
};
responseEntity = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, requestEntity, type);
现在一切正常。