You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 736bed6
Browse filesBrowse the repository at this point in the historyBrowse files
adrien caubel
committed
Provide a SlicedModel DTO to render stable JSON representations of Slice.
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/repositories/core-extensions-web.adoc
+53-3Lines changed: 53 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -195,18 +195,63 @@ This will result in a JSON structure looking like this:
195
195
196
196
Note how the document contains a `page` field exposing the essential pagination metadata.
197
197
198
+
[[core.web.page.sliced-model]]
199
+
=== Using Spring Data's `SlicedModel`
200
+
201
+
`Slice` instances have the same rendering-stability problem as `Page` instances, and `org.springframework.data.web.SlicedModel` is their equivalent DTO.
202
+
It is used in exactly the same way:
203
+
204
+
[source,java]
205
+
----
206
+
import org.springframework.data.web.SlicedModel;
207
+
208
+
@Controller
209
+
class MyController {
210
+
211
+
private final MyRepository repository;
212
+
213
+
// Constructor omitted
214
+
215
+
@GetMapping("/slice")
216
+
SlicedModel<?> slice(Pageable pageable) {
217
+
return new SlicedModel<>(repository.findAllBy(pageable)); // <1>
218
+
}
219
+
}
220
+
----
221
+
222
+
<1> Wraps the `Slice` instance into a `SlicedModel`.
223
+
224
+
This will result in a JSON structure looking like this:
225
+
226
+
[source,javascript]
227
+
----
228
+
{
229
+
"content" : [
230
+
… // Slice content rendered here
231
+
],
232
+
"page" : {
233
+
"size" : 20,
234
+
"number" : 0,
235
+
"hasNext" : true
236
+
}
237
+
}
238
+
----
239
+
240
+
The metadata is exposed under the same `page` field as for `PagedModel`.
241
+
As a `Slice` does not know the total number of elements, `totalElements` and `totalPages` are replaced by `hasNext`, which tells the client whether a further slice can be requested.
242
+
198
243
[[core.web.page.config]]
199
-
=== Globally enabling simplified `Page` rendering
244
+
=== Globally enabling simplified `Page` and `Slice` rendering
200
245
201
-
If you don't want to change all your existing controllers to add the mapping step to return `PagedModel` instead of `Page` you can enable the automatic translation of `PageImpl` instances into `PagedModel` by tweaking `@EnableSpringDataWebSupport` as follows:
246
+
If you don't want to change all your existing controllers to add the mapping step to return `PagedModel` instead of `Page` (or `SlicedModel` instead of `Slice`) you can enable the automatic translation of `PageImpl` instances into `PagedModel` and of `SliceImpl` instances into `SlicedModel` by tweaking `@EnableSpringDataWebSupport` as follows:
This will allow your controller to still return `Page` instances and they will automatically be rendered into the simplified representation:
254
+
This will allow your controller to still return `Page` and `Slice` instances and they will automatically be rendered into the simplified representation:
Serializing PageImpl instances as-is is not supported, meaning that there is no guarantee about the stability of the resulting JSON structure!
140
-
For a stable JSON structure, please use Spring Data's PagedModel (globally via @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO))
141
-
or Spring HATEOAS and Spring Data's PagedResourcesAssembler as documented in https://docs.spring.io/spring-data/commons/reference/repositories/core-extensions.html#core.web.pageables.
157
+
Serializing PageImpl and SliceImpl instances as-is is not supported, meaning that there is no guarantee about the stability of the resulting JSON structure!
158
+
For a stable JSON structure, please use Spring Data's PagedModel or SlicedModel (globally via @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO))
159
+
or Spring HATEOAS and Spring Data's PagedResourcesAssembler or SlicedResourcesAssembler as documented in https://docs.spring.io/spring-data/commons/reference/repositories/core-extensions.html#core.web.pageables.
0 commit comments