我在服务器中有以下控制器:
@RestController
class PersonController {
final PersonService personService;
@Autowired
PersonController( PersonService personService ){
this.personService = personService
}
@RequestMapping(value="/persons",method=RequestMethod.GET)
Page<Person> list( Pageable pageable){
Page<Person> persons = personService.listAllByPage(pageable);
}
}
json-response看起来像预期的那样。它包括内容和总页数:
{
"content": [
{
"id": 1234,
"name": "string",
},
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageSize": 5,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"totalPages": 1,
"totalElements": 1,
"last": true,
"number": 0,
"size": 5,
"sort": {
"sorted": false,
"unsorted": true
},
"numberOfElements": 1,
"first": true
}
关联的客户端方法如下所示:
public List<PersonDTO> findAllPerPage(final Pageable pageable) throws DataAccessException {
try {
UriComponentsBuilder builder = UriComponentsBuilder.
fromUri(restClient.getServiceURI(PERSON_URL))
.queryParam("page", pageable.getPageNumber())
.queryParam("size", pageable.getPageSize());
ResponseEntity<PagedResources<PersonDTO>> response =
restClient.exchange(
builder.toUriString(),
HttpMethod.GET,
null,
new ParameterizedTypeReference<PagedResources<PersonDTO>>() {
});
return new ArrayList<>(response.getBody().getContent());
} catch (Exception e) {
throw new DataAccessException(e.getMessage(), e);
}
}
我的问题是如何轻松地从响应中获取totalPages。问题是,响应的元数据设置为空,我不知道为什么?