Skip to content

Provide a SlicedModel DTO to render stable JSON representations of Slice #3516

Description

@adrien1212

org.springframework.data.web.PagedModel was introduced in 3.3 via #3024 as a middle ground between serializing PageImpl directly and adopting Spring HATEOAS. There is currently no equivalent for Slice.

Problem

The rationale in #3024 applies equally to slices. SliceImpl is a domain type, so its JSON representation carries the same stability risk that motivated PagedModel — but returning a Slice from a controller today leaves three options, none good:

  1. Serialize SliceImpl as-is, with the same instability PagedModel exists to avoid (and without the PlainPageSerializationWarning that flags the problem for pages).
  2. Adopt Spring HATEOAS and SlicedResourcesAssembler (Support to render SlicedModel from Slice via a dedicated RepresentationModelAssembler #1307), which is the documented answer but changes the payload to HAL.
  3. Create a dedicated custom SlicedModel DTO per project.

Proposal

A SlicedModel<T> in org.springframework.data.web, mirroring PagedModel in shape and usage:

@GetMapping("/slice")
SlicedModel<?> slice(Pageable pageable) {
    return new SlicedModel<>(repository.findAllBy(pageable));
}
{
  "content": [ ... ],
  "page": { "size": 20, "number": 0, "numberOfElements": 20, "hasNext": true }
}

Deliberately no totalElements/totalPages, since that information is exactly what a Slice does not have.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions