How can I use Pageable in @RestController?

Jin Kwon picture Jin Kwon · Nov 3, 2017 · Viewed 27.2k times · Source

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
}

Answer

StanislavL picture StanislavL · Nov 3, 2017

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