如何在@RestController 中使用 Pageable? [英] How can I use Pageable in @RestController?
本文介绍了如何在@RestController 中使用 Pageable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道 Pageable
来自 spring-data-
域.
I know the Pageable
comes from spring-data-
domain.
在@RestController
中是否有直接使用org.springframework.data.domain.Pageable
的优雅方式?
Is there any elegant way to directly use org.springframework.data.domain.Pageable
in @RestController
?
我尝试跟随.
@RequestMapping(method = RequestMethod.GET,
path = "pageable",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Pageable> readPageable(@NotNull final Pageable pageable) {
return ResponseEntity.ok(pageable);
}
结果出乎我的意料.
...: ~ $ curl -X GET --header 'Accept: application/json' 'http://localhost:8080/.../pageable?limit=1&offset=1' | python -mjson.tool
...
{
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"sort": null
}
推荐答案
返回的不是 Pageable 而是 Page.
It should return not Pageable but Page.
public Page<YourEntityHere> readPageable(@NotNull final Pageable pageable) {
return someService.search(pageable);
}
Pageable 是请求端,其中包含您所需要的内容.但 Page 包含结果.
Pageable is request side which contains what exactly you need. But Page contains results.
参见例如链接
这篇关于如何在@RestController 中使用 Pageable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文