What a page answers
public record PageDto<T>(List<T> items, int page, int totalPages, long totalElements) {}
{"items":[{"id":3,"name":"Alex Brown","email":"alex.brown@example.com","departmentName":"Support"},
{"id":7,"name":"Chris Evans","email":"chris.evans@example.com","departmentName":"Engineering"},
{"id":4,"name":"Emily Clark","email":"emily.clark@example.com","departmentName":"Finance"}],
"page":0,"totalPages":3,"totalElements":8}
public PageDto<EmployeeDto> findPage(Pageable pageable) {
Page<Employee> page = employeeRepository.findAll(pageable);
return new PageDto<>(employeeMapper.toDtoList(page.getContent()),
page.getNumber(), page.getTotalPages(), page.getTotalElements());
}
Page<Employee> is what the repository returns: the slice plus the counts. The service maps its content to DTOs and puts the four values the frontend needs into a record of its own; Page itself is not returned, because its JSON shape is Spring's and changes between versions.
Sorting and paging belong in the query. findAll() followed by subList in Java has already loaded every row.