How to do Pagination and Hateoas in JAX-RS/JEE?

Viewed 1775

Adding Pagination and HATEOAS to a REST-resource in Spring is quite simple and supported out of the box:

@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);
}

That's it. I can call localhost:8080/notes/pages?page=0&size=2and get the following JSON back:

{
"_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
}

}

How can I achieve the same with plain JavaEE and JAX-RS?

0 Answers
Related