Reputation: 1085
I am using Spring Boot 2 and I have write a @RestController
within a @GetMapping
that takes a Pageable
as parameter.
@GetMapping
public ResponseEntity<Page<AppointmentTO>> findAll(Pageable pageable) {
Page<AppointmentTO> page = appointmentService.findAll(pageable);
return ResponseEntity.ok(page);
}
The problem is the following:
By each request, the queries-parameters pageSize
and offset
are always reset to default when they arrived in Spring Boot Backend (?offset=0&pageSize=20
), however I send different parameters in the url of my request (?offset=15&pageSize=5
for example).
Upvotes: 2
Views: 7990
Reputation: 2209
Spring boot Pageable
supports the following url params OOTB:
Sample endpoint:
@GetMapping
public ResponseEntity<?> getUsers(Pageable pageable) {
try {
return ResponseEntity.status(HttpStatus.OK).body(userService.getUsers(pageable));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
Sample Request:
localhost:8080/users?page=2&size=5&sort=createdAt,DESC
Sample Response (note the paging data below):
{
"content": [
...
],
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 10,
"pageNumber": 2,
"pageSize": 5,
"paged": true,
"unpaged": false
},
"last": false,
"totalPages": 12,
"totalElements": 58,
"size": 5,
"number": 2,
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"numberOfElements": 5,
"first": false,
"empty": false
}
Upvotes: 0
Reputation: 3572
Spring Boot maps the request params to org.springframework.data.domain.PageRequest
that extends AbstractPageRequest
AbstractPageRequest implements Pageable, Serializable {
...
private final int page;
private final int size;
public long getOffset() {
return (long)this.page * (long)this.size;
}
...
You should use following url:
http://localhost:8080?page=3&size=5
Also you could add sorting by ...&sort=name
Upvotes: 8