春季HATEOAS用于分页

huangapple 未分类评论56阅读模式
英文:

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&lt;TeamDTO&gt; getListOfTeam(int page) {
        Pageable pageable = PageRequest.of(page, 8);
        Page&lt;TeamEntity&gt; result = teamRepository.findAll(pageable);
        return result.map(teamEntity -&gt; 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&lt;PagedModel&lt;TeamDTO&gt;&gt; getListOfTeam(@RequestParam(value = &quot;page&quot;, defaultValue = &quot;0&quot;, required = false) int page,PagedResourcesAssembler assembler) {
        Page&lt;TeamDTO&gt; teams = teamService.getListOfTeam(page);
        PagedModel&lt;TeamDTO&gt; pr = assembler.toModel(teams);

        return new ResponseEntity&lt;&gt;(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 -->

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

<!-- end snippet -->

But this was what I wanted to achieve

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

{
&quot;links&quot;: [
        {
          &quot;rel&quot;: &quot;first&quot;,
          &quot;href&quot;: &quot;http://localhost:8080/team?page=0&amp;size=8&quot;
        },
        {
          &quot;rel&quot;: &quot;self&quot;,
          &quot;href&quot;: &quot;http://localhost:8080/team?page=0&amp;size=8&quot;
        },
        {
          &quot;rel&quot;: &quot;next&quot;,
          &quot;href&quot;: &quot;http://localhost:8080/team?page=1&amp;size=8&quot;
        },
        {
          &quot;rel&quot;: &quot;last&quot;,
          &quot;href&quot;: &quot;http://localhost:8080/team?page=4&amp;size=8&quot;
        }
],
&quot;_embedded&quot;:{
    &quot;teams&quot;:[
        {
          &quot;teamId&quot;: 1,
          &quot;teamName&quot;: &quot;string&quot;,
          &quot;status&quot;: &quot;string&quot;,
          &quot;deliveryBoyMergerDTOList&quot;: [],
          &quot;links&quot;: []
        },
        {
          &quot;teamId&quot;: 8,
          &quot;teamName&quot;: &quot;string&quot;,
          &quot;status&quot;: &quot;string&quot;,
          &quot;deliveryBoyMergerDTOList&quot;: [],
          &quot;links&quot;: []
        }
],
&quot;page&quot;: {
      &quot;size&quot;: 8,
      &quot;totalElements&quot;: 36,
      &quot;totalPages&quot;: 5,
      &quot;number&quot;: 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 = &quot;teams&quot;) to your TeamDTO class

huangapple
  • 本文由 发表于 2020年4月6日 22:23:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/61062023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定