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());
}
}
Request /api/test?page=1&size=2 returns entire init list (from 1 to 10).
From logs:
Enter: com.test.rest.TestResource.test() with argument[s] = [Page request [number: 1, size 2, sort: null]]
How to make it work?
Let's break down your calls.
PageImpl's Constructor
Chunk's Constructor
PageImpl doesn't implement this method, but Chunk does.
In summary, you set the value of content when you passed in
init
as a parameter to the constructor. When you calledgetContent()
, you get more-or-less exactly what you put in originally.As I understood, Page works with repositories only, so I found a solution:
Use org.springframework.beans.support.PagedListHolder
Request /api/test?page=1&size=2 returns [3,4]