如何在JAX-RS / JEE中进行分页和仇恨?

时间:2017-08-04 15:02:00

标签: java-ee pagination jax-rs hateoas

在Spring中将分页和HATEOAS添加到REST资源非常简单,并且支持开箱即用:

@RequestMapping(value = "/pages", method = RequestMethod.GET)
PagedResources<NoteResource> allPaged(@PageableDefault Pageable p, PagedResourcesAssembler<Note> pagedAssembler) {
    Page<Note> pageResult = noteRepository.findAll(p);
    return pagedAssembler.toResource(pageResult, noteResourceAssembler);
}

那就是它。我可以调用localhost:8080/notes/pages?page=0&size=2并获得以下JSON:

{
"_embedded": {
    "noteList": [
        {
            "id": 1,
            "title": "firstNote",
            "body": "lala",
            "_links": {
                "self": {
                    "href": "http://localhost:8080/notes/1"
                },
                "note-tags": {
                    "href": "http://localhost:8080/notes/1/tags"
                }
            }
        },
        {
            "id": 2,
            "title": "secondNote",
            "body": "blabla",
            "_links": {
                "self": {
                    "href": "http://localhost:8080/notes/2"
                },
                "note-tags": {
                    "href": "http://localhost:8080/notes/2/tags"
                }
            }
        }
    ]
},
"_links": {
    "first": {
        "href": "http://localhost:8080/notes/pages?page=0&size=2"
    },
    "self": {
        "href": "http://localhost:8080/notes/pages?page=0&size=2"
    },
    "next": {
        "href": "http://localhost:8080/notes/pages?page=1&size=2"
    },
    "last": {
        "href": "http://localhost:8080/notes/pages?page=5&size=2"
    }
},
"page": {
    "size": 2,
    "totalElements": 12,
    "totalPages": 6,
    "number": 0
}

}

如何使用普通JavaEE和JAX-RS实现相同的目标?

0 个答案:

没有答案