英文:
Spring HATEOAS for pagination
问题
我使用Spring HATEOAS来支持我的应用程序中的分页功能。在做了一些研究之后,我最终做了类似以下的操作。
我从我的服务类返回了一个页面
@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));
}
然后使用了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);
}
最终,我得到了类似以下的结果
{
"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": []
},
//其余的项
],
"page": {
"size": 8,
"totalElements": 36,
"totalPages": 5,
"number": 0
}
}
但这并不是我想要的结果
{
"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
}
}
我对链接没什么问题,但我希望键名是我返回的实体的名称,而不是 'content'。我找不到任何示例或可供参考的来源。我不太确定该如何继续实现我想要的效果。是否有任何参考资料或建议可以帮助我?谢谢。
英文:
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
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
@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));
}
<!-- end snippet -->
And used PagedModel to add necessary links
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
@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);
}
<!-- end snippet -->
I ended up getting something like this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
{
"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
}
}
<!-- end snippet -->
But this was what I wanted to achieve
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
{
"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
}
}
<!-- end snippet -->
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
得分: 0
你可以通过将 @Relation(collectionRelation = "teams")
添加到你的 TeamDTO
类来实现这一点。
英文:
> 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
专注分享java语言的经验与见解,让所有开发者获益!
评论