Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring HATEOAS build link to paged resource

I have a controller with method, which returns PagedResource, which looks like this:

@RequestMapping(value = "search/within", method = RequestMethod.POST)
public @ResponseBody PagedResources within(@RequestBody GeoJsonBody body,
                                           Pageable pageable, PersistentEntityResourceAssembler asm) {

    //  GET PAGE

    return pagedResourcesAssembler.toResource(page, asm);
}

Now, I want to add that method as a link to the root resource, so I do the following:

public RepositoryLinksResource process(RepositoryLinksResource repositoryLinksResource) {
    repositoryLinksResource.add(linkTo(methodOn(ShipController.class).within(null, null, null)).withRel("within"));

    return repositoryLinksResource;
}

Which works and I get my link, however it add that link without pagination parameters. So it look like this:

    "within": {
        "href": "http://127.0.0.1:5000/search/within"
    },

and I want to turn it into:

    "within": {
        "href": "http://127.0.0.1:5000/search/within{?page, size}"
    },

This previous question on stackoverflow suggests that after fixing the corresponding issue on GitHub it should work by default, however, it doesn't.

What am I doing wrong ?

like image 479
Виталик Бушаев Avatar asked Jul 26 '18 15:07

Виталик Бушаев


1 Answers

Automatic Creation of Paginated Links with PagedResourcesAssembler

I had success using PagedResourcesAssembler. Let's say your entity is called MyEntity. Your within method should return HttpEntity<PagedResources<MyEntity>>.

Your within method should look something similar to the example shown below.

@RequestMapping(value = "search/within", method = RequestMethod.POST)
@ResponseBody 
public HttpEntity<PagedResources<MyEntity>> 
    within(@RequestBody GeoJsonBody body,Pageable pageable, 
           PagedResourcesAssembler assembler) {
    //  GET PAGE
    Page<MyEntity> page = callToSomeMethod(pageable);

    return new ResponseEntity<>(assembler.toResource(page), HttpStatus.OK);
}

Here is a simple example. In this example, the response looked like the one shown below,

{
    "_embedded": {
    "bookList": [
      {
        "id": "df3372ef-a0a2-4569-982a-78c708d1f609",
        "title": "Tales of Terror",
        "author": "Edgar Allan Poe"
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/books?page=0&size=20"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 1,
    "totalPages": 1,
    "number": 0
  }
}

Manual Creation of Paginated Self Link

If you're interested in creating the paginated link manually, here's the code snippet you can use,

Page<MyEntity> page = callToSomeMethod(pageable);

ControllerLinkBuilder ctrlBldr =
    linkTo(methodOn(ShipController.class).within(body, pageable, asm));
UriComponentsBuilder builder = ctrlBldr.toUriComponentsBuilder();

int pageNumber = page.getPageable().getPageNumber();
int pageSize = page.getPageable().getPageSize();
int maxPageSize = 2000;

builder.replaceQueryParam("page", pageNumber);
builder.replaceQueryParam("size", pageSize <= maxPageSize ? 
    page.getPageable().getPageSize() : maxPageSize);

Link selfLink =
    new Link(new UriTemplate(builder.build().toString()), "self");
like image 53
Indra Basak Avatar answered Nov 14 '22 11:11

Indra Basak