本文介绍了如何在@RestController中使用Pageable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Pageable来自spring-data-域.

@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);
}

可分页是请求方,其中包含您真正需要的内容.但是Page包含结果.

Pageable is request side which contains what exactly you need. But Page contains results.

例如,参见链接

这篇关于如何在@RestController中使用Pageable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:41