SpringBoot Pagination - PullRequest
       5

SpringBoot Pagination

0 голосов
/ 06 ноября 2019

Я перестраиваю бэкэнд, и у меня нет самого кода, только вызовы. Сервер был сделан с Laravel, и один из вызовов это -> http://laravel.austinradams.com/news

Он возвращает код Json:

{
"success": true,
"prev": 1,
"current": 1,
"next": 2,
"last": 4,
"data": [
    {
        "id": 43,
        "author": "Vox",
        "title": "[Resolved] Important Notice Regarding Voting and NX",
        "type": "General",
        "content": "<p><strong>[Resolved]&nbsp;</strong>We have resolved this issue.</p><p>Hey players,</p><p>We are aware of a situation regarding our voting system where you are not receiving the NX you should be getting. <b>Rest assured, your votes are still being counted and your NX will be distributed at a later time.</b></p><p><b>To reiterate, your NX will be distributed at a later time. We'll update this post and on the Discord & Forums when we have more information.</b></p><p>We apologize for the inconvenience and thank you for your patience.</p><p>- Lumiere Network</p>",
        "views": "335",
        "created_at": "2017-05-03 15:44:31",
        "updated_at": "2017-07-08 02:54:15"
    },
    {
        "id": 48,
        "author": "Austin",
        "title": "Test",
        "type": "Announcement",
        "content": "test",
        "views": "118",
        "created_at": "2017-05-26 23:58:15",
        "updated_at": "2017-07-08 02:41:43"
    },
    {
        "id": 17,
        "author": "Alan",
        "title": "Advertise / Recommend Event",
        "type": "Event",
        "content": "SlimeTales has officially opened up but it's now up to the effort of the community in order to grow!  Help us advertise our server by streaming / making videos / etc, or simply recommend it to a few of your friends to help the SlimeTales community grow.  List of prizes:  5 People - 1 Legendary Lotto 10 People - 30 day Kino Pet (with auto loot) 15 People - HalfBlood Wings   * All items may only be claimed once.  ** Players must be new to SlimeTales and will not apply to existing users.  Please follow the guildlines on the following post link provided.",
        "views": "89",
        "created_at": "2017-04-10 01:29:16",
        "updated_at": "2017-07-07 16:09:21"
    },
    {
        "id": 41,
        "author": "Vox",
        "title": "Check out the Benefits as a Streamer!",
        "type": "General",
        "content": "<p>Hey players,</p><p>Like streaming? Show off it all off with a special Discord role on our Discord chat!<p><p>To register as a streamer on our Discord chat,</p><ol><li>Head to our Discord.</li><li>PM \"Phantom\" so he can set your role.</li><li>Dance and party away! Woo!</li></ol><p>Thanks for supporting us!</p>",
        "views": "68",
        "created_at": "2017-05-03 15:44:31",
        "updated_at": "2017-07-06 01:19:25"
    },
    {
        "id": 49,
        "author": "testalana",
        "title": "test",
        "type": "General",
        "content": "testtesttesttesttest\ntest",
        "views": "73",
        "created_at": "2017-05-27 00:43:45",
        "updated_at": "2017-07-06 01:19:24"
    }
]}

Хорошо, но на моем сервере у меня естьSpringBoot и я возвращаю объект Page.

Мой код выглядел так:

@GetMapping
public Page<News> getAllEmployees(
        @RequestParam(defaultValue = "0") Integer pageNo,
        @RequestParam(defaultValue = "1") Integer pageSize,
        @RequestParam(defaultValue = "id") String sortBy) {

    List<News> list = newsService.getAllEmployees(pageNo, pageSize, sortBy);

    Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(sortBy));

    Long start = pageable.getOffset();
    Long end = (start + pageable.getPageSize()) > list.size() ? list.size() : (start + pageable.getPageSize());
    return new PageImpl<>(list.subList(start.intValue(), end.intValue()), pageable, list.size());

}

Сгенерировано json:

{
"content": [
    {
        "id": 2,
        "author": "Rodrigo",
        "content": "aaaaa",
        "createdAt": 1572919200000,
        "title": "bbbbb",
        "type": "2",
        "updatedAt": 1572919200000,
        "views": 1
    }
],
"pageable": {
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "offset": 0,
    "pageSize": 1,
    "pageNumber": 0,
    "unpaged": false,
    "paged": true
},
"totalElements": 1,
"totalPages": 1,
"last": true,
"size": 1,
"number": 0,
"sort": {
    "sorted": true,
    "unsorted": false,
    "empty": false
},
"numberOfElements": 1,
"first": true,
"empty": false

}

Хорошо, я знаю, что вы вернули только один, потому что я передал его по умолчанию:

@RequestParam(defaultValue = "1") Integer pageSize

Есть ли способ, которым Json похож на то, что Laravel генерировал с атрибутами успеха prev , current , next , last ( Data` будет содержимым), или мне придется переделывать фронт?

Примечание: я также знаю, что возвращаемые датыВ зависимости от типа данных объекты обрабатываются, так что это не имеет значения, поскольку я изменяю их.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...