Search code examples
javaspringspring-mvcspring-hateoas

In spring-hateoas there is PagedResources but not SlicedResources, is there a recommended way to handle Slice Metadata?


I also saw a ver similar question in the spring-hateoas repository in github but the answer was to ask in stackoverflow.

I'm using spring-hateoas PagedResources in my project for the return response of API endpoints. I now have an API endpoint where the count information will not be available so I'm using a Slice instead of a Page. However, when building the response I did not find a SlicedResources, only PagedResources class, and the PagedResources class requires information that I don't want to query for or send (like total number of elements).

This is what I do to create the metadata response for pages:

final PageMetadata metadata = new PageMetadata(page.getSize(), page.getNumber(), page.getTotalElements(), page.getTotalPages());

I was wondering if there is a recommended way to do what I'm doing for pages but for Slices instead, or if this is just not implemented in spring-hateoas.


Solution

  • I know that has passed some time since the question, but there are people still asking about it, so I note down my findings with a recent release of HATEOAS.

    As of HATEHOAS 1.2 there is not support for somtething like

        @Override
        public SlicedModel<EntityModel<T>> toModel(Slice<T> entity) {
            return toModel(entity, EntityModel::of);
        }
    

    and the issue DATACMNS-845 in spring-data-commons project in git is open but seems to be dormant, so I'd say that there is few chances that this see the light. I am probably missing something, but considering that for large collections getting all the information for pages can be an expensive operation, I find that providing support for slices would be good.