Skip to content

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

Open
arnabnandy7 wants to merge 1 commit into
spring-projects:mainfrom
arnabnandy7:feature/slicedModel
Open

arnabnandy7 wants to merge 1 commit into
spring-projects:mainfrom
arnabnandy7:feature/slicedModel

Conversation

@arnabnandy7

Copy link
Copy Markdown

Provide a SlicedModel DTO to render stable JSON representations of Slice.

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 was previously no equivalent for Slice.

Problem

Returning a Slice from a Spring MVC controller carried stability risks:

  1. SliceImpl is a domain type and serializing it directly exposes internal structure that can change across releases.
  2. In DIRECT mode, no warning was emitted for SliceImpl instances.
  3. Applications not using Spring HATEOAS (SlicedResourcesAssembler) had to define custom DTOs per project to wrap Slice.

Solution

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

  • SlicedModel<T> and SliceMetadata:
    • Exposes content and page metadata (size, number, numberOfElements, hasNext).
    • Deliberately omits totalElements and totalPages as that information is unavailable on a Slice.
    • Implements equals(Object) and hashCode().
  • Jackson Integration (Jackson 2 & 3):
    • In PageSerializationMode.VIA_DTO, registers a mix-in on SliceImpl.class with SliceModelConverter to wrap SliceImpl instances into SlicedModel.
    • In PageSerializationMode.DIRECT, emits a one-time warning log highlighting stability risks and suggesting SlicedModel or SlicedResourcesAssembler.
  • Web Support & Javadoc:
    • Updated @EnableSpringDataWebSupport and PageSerializationMode Javadocs to document SliceImpl and SlicedModel.
  • AOT / Native Hints:
    • Registered reflection hints in WebRuntimeHints for SlicedModel, SliceMetadata, and SliceModelConverter.
  • Documentation & Tests:
    • Added Antora reference documentation for SlicedModel in core-extensions-web.adoc and aot.adoc.
    • Added unit and integration tests across SlicedModelUnitTests, PageImplJsonSerializationUnitTests, PageImplJsonJackson2SerializationUnitTests, EnableSpringDataWebSupportIntegrationTests, and WebRuntimeHintsUnitTests.

Closes #3516

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

…ice.

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a SlicedModel DTO to render stable JSON representations of Slice

2 participants