如何使用Page <t>使用Resource <t>进行分页

时间:2018-08-22 20:42:51

标签: rest spring-mvc pagination spring-data

我正在使用以下代码为Artist对象创建JSON数据的分页列表:

@RequestMapping(value = "/artists/all", method = RequestMethod.GET, produces = {"application/hal+json"})
public Resources<Artist> getArtists(Pageable pageable) {
    final Page<Artist> allArtists= artistService.GetAllArtists(pageable);

    for (final Artist artist : allArtists) {
        Long artistId = artist.getArtistId();
        Link selfLink = linkTo(methodOn(ArtistController.class).getAllArtistById(artistId)).withSelfRel();
        artist.add(selfLink);
        final Link ordersLink = linkTo(methodOn(MusicController.class).getMusicByArtistId(artistId, pageable)).withRel("musics");
        artist.add(ordersLink);
    }

    Link link =linkTo(ArtistController.class).withSelfRel();
    Resources<Artist> result = new Resources<>(allArtists,link);
    return result;
}

当前代码返回以下格式的输出:

{
  "artistId": 2, 
  "name": "Simon Mburu", 
  "_links": {
    "self": {
      "href": "http://localhost:8000/api/v1/artists/2"
    },
    "musics": {
      "href": "http://localhost:8000/api/v1/musics/artist/2"
    }
  }
}

但是我的目的是让代码返回如下输出:

"pageable": {
  "sort": {
    "sorted": false, 
    "unsorted": true
  },
  "offset": 0,
  "pageSize": 20,
  "pageNumber": 0, 
  "paged": true,
  "unpaged": false
},
"last": true,
"totalPages": 1, 
"totalElements": 2,
"size": 20
"number": 0, 
"numberOfElements": 2
"sort": {
     // ETC...

我可以对代码进行哪些更改以使其输出上述示例中包含的数据而不是当前输出的数据?

0 个答案:

没有答案