简体   繁体   English

Spring HATEOAS 用于分页

[英]Spring HATEOAS for pagination

I was thinking of using Spring HATEOAS to support pagination in my application.After some research I ended up doing something like this.我正在考虑使用 Spring HATEOAS 来支持我的应用程序中的分页。经过一些研究,我最终做了这样的事情。

I returned page from my service class我从我的服务 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并使用 PagedModel 添加必要的链接

 @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我对链接没问题,但我希望密钥是我要返回的实体的名称,而不是“内容”。我找不到任何可以遵循的示例或来源。我不太确定我应该如何继续实现我正在寻找的东西。我可以照顾的任何参考材料或任何建议都会有很大帮助。谢谢

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您可以通过将@Relation(collectionRelation = "teams")添加到您的TeamDTO class 来实现此目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM