Is there a way to simplify the structure returned from this controller:
@GetMapping
public Iterable getAll(@PathParam("page") int page)
Another way is to create a custom page class as you want and create from your data and return
@Data
public class CustomPage {
List content;
CustomPageable pageable;
public CustomPage(Page page) {
this.content = page.getContent();
this.pageable = new CustomPageable(page.getPageable().getPageNumber(),
page.getPageable().getPageSize(), page.getTotalElements());
}
@Data
class CustomPageable {
int pageNumber;
int pageSize;
long totalElements;
public CustomPageable(int pageNumber, int pageSize, long totalElements) {
this.pageNumber = pageNumber;
this.pageSize = pageSize;
this.totalElements = totalElements;
}
}
}
And use like this
@GetMapping
public CustomPage getAll(@PathParam("page") int page) {
Pageable pageable = PageRequest.of(page, 3);
return new CustomPage(taskRepository.findAll(pageable));
}
Note: Here @Data of Lombok used for setter getter etc.