Spring HATEOAS for pagination

Viewed 1504

I was thinking of using Spring HATEOAS to support pagination in my application.After some research I ended up doing something like this .

I returned page from my service class

 @Override
    public Page<TeamDTO> getListOfTeam(int page) {
        Pageable pageable = PageRequest.of(page, 8);
        Page<TeamEntity> result = teamRepository.findAll(pageable);
        return result.map(teamEntity -> toDTO(teamEntity));
    }

And used PagedModel to add necessary links

@GetMapping
    public ResponseEntity<PagedModel<TeamDTO>> getListOfTeam(@RequestParam(value = "page", defaultValue = "0", required = false) int page,PagedResourcesAssembler assembler) {
        Page<TeamDTO> teams = teamService.getListOfTeam(page);
        PagedModel<TeamDTO> pr = assembler.toModel(teams);

        return new ResponseEntity<>(assembler.toModel(teams),HttpStatus.OK);
    }

I ended up getting something like this

{
  "links": [
      {
        "rel": "first",
        "href": "http://localhost:8080/team?page=0&size=8"
      },
      {
        "rel": "self",
        "href": "http://localhost:8080/team?page=0&size=8"
      },
      {
        "rel": "next",
        "href": "http://localhost:8080/team?page=1&size=8"
      },
      {
        "rel": "last",
        "href": "http://localhost:8080/team?page=4&size=8"
      }
],
"content": [
      {
        "teamId": 1,
        "teamName": "string",
        "status": "string",
        "deliveryBoyMergerDTOList": [],
        "links": []
      },
      {
        "teamId": 2,
        "teamName": "string",
        "status": "string",
        "deliveryBoyMergerDTOList": [],
        "links": []
      },
      //rest of items
],
"page": {
      "size": 8,
      "totalElements": 36,
      "totalPages": 5,
      "number": 0
}
}

But this was what I wanted to achieve

{
"links": [
        {
          "rel": "first",
          "href": "http://localhost:8080/team?page=0&size=8"
        },
        {
          "rel": "self",
          "href": "http://localhost:8080/team?page=0&size=8"
        },
        {
          "rel": "next",
          "href": "http://localhost:8080/team?page=1&size=8"
        },
        {
          "rel": "last",
          "href": "http://localhost:8080/team?page=4&size=8"
        }
],
"_embedded":{
    "teams":[
        {
          "teamId": 1,
          "teamName": "string",
          "status": "string",
          "deliveryBoyMergerDTOList": [],
          "links": []
        },
        {
          "teamId": 8,
          "teamName": "string",
          "status": "string",
          "deliveryBoyMergerDTOList": [],
          "links": []
        }
],
"page": {
      "size": 8,
      "totalElements": 36,
      "totalPages": 5,
      "number": 0
}
}

I am okay with links but I wanted the key to be the name of the entity I am returning rather than 'content' .I couldn't find any examples or sources I could follow along.I am not pretty sure how I should proceed to achieve what I am looking for.Any reference material I could look after or any suggestion would be of great help .Thank you

1 Answers

I am okay with links but I wanted the key to be the name of the entity I am returning rather than 'content'

You can achieve this by adding @Relation(collectionRelation = "teams") to your TeamDTO class

Related