0

I have the following controller in the server:

@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);
   } 
}

The json-response looks as expected. It includes the content and the total number of pages:

{
    "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
}

The associated client method looks like this:

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);
    }
}

My question is how to get the totalPages out of the response easily. The problem is, that the meta-data of the response is set to null and I don't know why?

Steve2Fish
  • 187
  • 4
  • 15
  • What exactly do you mean by "the metadata of the response'? – tgdavies May 05 '18 at 06:01
  • metadata of paged response: https://docs.spring.io/spring-hateoas/docs/current/api/org/springframework/hateoas/PagedResources.PageMetadata.html – Steve2Fish May 05 '18 at 06:43
  • I think I have solved the problem. I now use RestResponsePage instead of PagedResources like here: https://stackoverflow.com/questions/34647303/spring-resttemplate-with-paginated-api And now I am able to get the totalPages. – Steve2Fish May 05 '18 at 06:45

0 Answers0