Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Fix Spring HATEOAS Pagination for Custom Repository Results

A custom repository must return a correctly populated Spring Data Page before PagedResourcesAssembler can build a HATEOAS PagedModel with navigation links.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

PagedModel does not paginate a custom repository result on its own. To return Spring HATEOAS navigation links, have the repository provide a correctly populated Spring Data Page<T>, then pass that page to PagedResourcesAssembler.toModel(...). First check which PagedModel class you imported: Spring Data’s simplified metadata model and Spring HATEOAS’s linked representation are separate types.

Why a custom repository result does not become a paginated HATEOAS response

Several different problems can look like “PagedModel doesn’t work.” The repository may return a list or custom DTO instead of a Spring Data Page; the endpoint may import the other PagedModel; or a representation may be constructed without page metadata or navigation links. Returning a model object does not itself run the HATEOAS pagination-link assembly step.

As an Amazon Associate I earn from qualifying purchases.

Spring Data’s PagedResourcesAssembler API documents conversion from a Spring Data Page<T> to a HATEOAS PagedModel. A list does not, in general, establish the total number of matching records or the metadata needed to describe a page correctly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check which PagedModel you mean

The two similarly named classes have different purposes. Verify the fully qualified import in your controller and the response contract expected by the client.

Type or approach Input What it provides Use it when
org.springframework.hateoas.PagedModel with PagedResourcesAssembler Spring Data Page<T> HATEOAS representation with page navigation links assembled from the request or a supplied self link Clients need hypermedia navigation
org.springframework.data.web.PagedModel Spring Data Page<T> Simplified page JSON with metadata; it does not provide HATEOAS navigation links You need a stable metadata envelope, not hypermedia links
Manually created HATEOAS PagedModel Content, explicit metadata, and optional links Only the metadata and links you supply Your application deliberately owns and validates the representation
SlicedResourcesAssembler Spring Data Slice<T> Slice-oriented HATEOAS representation without claiming a total-page count The query does not need a total count

Spring Data documents its simplified model and the VIA_DTO page serialization option in the Spring Data JPA reference. Spring HATEOAS documents its separate PagedModel as a representation for pageable collection responses in the PagedModel API.

Return a Page from the custom query, then assemble it

Keep custom filtering and query logic in the repository. At the endpoint boundary, return a Spring Data Page<T> whose content, requested Pageable, and total count reflect the query, then convert it with the assembler.

@GetMapping("/items")
PagedModel<EntityModel<Item>> items(
        Pageable pageable,
        PagedResourcesAssembler<Item> assembler) {
    Page<Item> page = repository.findCustomItems(pageable);
    return assembler.toModel(page);
}

This is an illustrative shape, not a tested implementation. Adapt the element representation and endpoint links to your application, and confirm the APIs against your project’s dependency versions. Spring Data’s PagedResourcesAssembler documentation describes the Page-to-model conversion and assembler overloads.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a valid path if the repository returns a List or Slice

If you have a List

A list alone cannot generally tell the framework how many matching records exist beyond the returned content. Change the repository method to return a suitable Page<T>, including a total count appropriate to the query, or intentionally construct a representation with metadata and links your application can substantiate. Spring HATEOAS exposes PagedModel.of(content, metadata, links) for explicit construction; see the PagedModel API.

If you have a Slice

When the query does not require a total count, use a slice-oriented response rather than presenting an unknown total as if it were known. The Spring Data JPA reference describes SlicedResourcesAssembler as the analogous conversion for a Slice and SlicedModel.

Check how pagination links are based

PagedResourcesAssembler can be configured with a pageable resolver and base URI. If its base URI is null, its API says toModel(Page) uses the current request URI to build relevant previous and next links. Overloads also accept a self link, letting the application choose a different link base.

The default assembler targets the controller method where it is invoked. Generated query parameters follow the pageable resolver configuration, so custom parameter names and defaults need to match the resolver used for link generation. See the assembler API and the Spring Data reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use simplified page JSON only when links are not required

Spring Data’s org.springframework.data.web.PagedModel wraps a Page in a simplified metadata representation. The reference also documents @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO) for simplified rendering of returned Page instances. This route does not add the navigation links produced by the Spring HATEOAS assembler; select it only if that matches the client contract.

Confirm version compatibility

The cited Spring Data Commons API page is labeled 4.1.0, and the Spring HATEOAS API pages are labeled 3.1.1; the Spring Data JPA reference is current. Your project’s versions were not specified, so check that the relevant overloads and configuration options exist in your dependencies before using the example unchanged.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.