gpt4 book ai didi

Spring分页不起作用

转载 作者:行者123 更新时间:2023-12-02 04:07:23 24 4
gpt4 key购买 nike

Controller :

@RestController
@RequestMapping("/api")
public class TestResource {

@RequestMapping(value="/test",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Integer>> test(Pageable pageable) {
List<Integer> init = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9,10}));
return ResponseEntity.ok(new PageImpl<>(init, pageable, init.size()).getContent());
}
}

请求/api/test?page=1&size=2返回整个初始化列表(从1到10)。
来自日志:

Enter: com.test.rest.TestResource.test() with argument[s] = [Page request [number: 1, size 2, sort: null]]

如何让它发挥作用?

最佳答案

据我了解,Page 仅适用于存储库,因此我找到了解决方案:

使用org.springframework.beans.support.PagedListHolder

public ResponseEntity<List<Integer>> getIntegers(Pageable pageable) {
List<Integer> init = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9,10}));
PagedListHolder<Integer> holder = new PagedListHolder<>(init);
holder.setPage(pageable.getPageNumber());
holder.setPageSize(pageable.getPageSize());
return ResponseEntity.ok(holder.getPageList());
}

请求/api/test?page=1&size=2返回[3,4]

关于Spring分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38839023/

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