gpt4 book ai didi

java - 在http get请求中设置对象

转载 作者:行者123 更新时间:2023-12-01 11:17:57 25 4
gpt4 key购买 nike

我正在为可分页Spring数据资源编写客户端java程序存储库 GET API 的格式为:

public Page<Person> findByCountry(..., Pageable pageable);

我已经创建了以下格式的可分页请求

Pageable pageable = new PageRequest(1, 40, new Sort("name"));

但是,我想知道如何将这个可分页设置到我的 http 客户端 中。我正在使用 HttpGetHttpPost

我用谷歌搜索了一下,发现大多数链接都使用 Spring 休息 Controller 。我不能使用 apache http 客户端来做到这一点吗?温泉仇恨似乎是另一种选择。但这意味着向我的客户项目添加另一个 jar。这只是如何设置请求负载的问题吗?

此外,如果有更好的方法请告诉我。我想循环遍历所有页面,直到数据耗尽。

谢谢。

最佳答案

我不确定我要建议的是否是最好的方法,但这就是我会采取的方法。由于如果不向客户端添加 Spring 依赖项,您就无法引用 Spring 依赖对象,即精益客户端中的 Pageable,因此您可能希望将 PageRequest 详细信息作为 HTTP GET 查询参数发送到 HTTP GET API,如下所示,然后循环遍历您的技术独立的分页器结果如下所示。

API签名

public PageWrapper findByCountry(..., int page, int size, String sortBy, String sortDirection);

PageWrapper:

List<T> items;
int currentPage;
int totalPages;
int totalItems;
boolean isLast;

API代码:

Pageable nextPageable = new PageRequest(page, size, new Sort(sortBy));
Page<Person> personsPage = countryService.findByCountry(.., nextPageable);
if(null != personsPage){
PageWrapper<Person> pageWrapper = new PageWrapper<>();
pageWrapper.setItems(personsPage.getContent());
pageWrapper.setTotalPages(personsPage.getTotalPages());
pageWrapper.setTotalItems(personsPage.getTotalElements());
pageWrapper.setCurrentPage(personsPage.getNumber())
pageWrapper.setIsLast(personsPage.isLast());
return pageWrapper
}

客户端代码:

PageWrapper page = null;
do {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("page", index++);
paramMap.put("size", size);
paramMap.put("sortBy", "name");
page = (PageWrapper) httpClient.get(requestURI, paramMap);
process(page);
}while(!page.getIsLast())

关于java - 在http get请求中设置对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31567075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com