How can I use Pageable in @RestController?

 ̄綄美尐妖づ 提交于 2019-12-23 12:19:06

问题


I know the Pageable comes from spring-data- domain.

Is there any elegant way to directly use org.springframework.data.domain.Pageable in @RestController?

I tried following.

@RequestMapping(method = RequestMethod.GET,
                path = "pageable",
                produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Pageable> readPageable(@NotNull final Pageable pageable) {
    return ResponseEntity.ok(pageable);
}

The result is not what I expected.

...: ~ $ 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
}

回答1:


It should return not Pageable but Page.

public Page<YourEntityHere> readPageable(@NotNull final Pageable pageable) {
    return someService.search(pageable);
}

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

See for example the link



来源:https://stackoverflow.com/questions/47087415/how-can-i-use-pageable-in-restcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!