Skip to content

Commit a5de842

Browse files
committed
Update section of the reference documentation regarding Page and Slice.
Update to latest addition in support for Slice. Also update to renamed methods and types in Spring HATEOAS. Related ticket: #1307
1 parent 116730c commit a5de842

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/main/asciidoc/repositories.adoc

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,11 +1542,12 @@ You have to populate `thing1_page`, `thing2_page`, and so on.
15421542
The default `Pageable` passed into the method is equivalent to a `PageRequest.of(0, 20)`, but you can customize it by using the `@PageableDefault` annotation on the `Pageable` parameter.
15431543

15441544
[[core.web.pageables]]
1545-
==== Hypermedia Support for Pageables
1545+
==== Hypermedia Support for `Page` and `Slice`
15461546

1547-
Spring HATEOAS ships with a representation model class (`PagedResources`) that allows enriching the content of a `Page` instance with the necessary `Page` metadata as well as links to let the clients easily navigate the pages.
1548-
The conversion of a `Page` to a `PagedResources` is done by an implementation of the Spring HATEOAS `ResourceAssembler` interface, called the `PagedResourcesAssembler`.
1549-
The following example shows how to use a `PagedResourcesAssembler` as a controller method argument:
1547+
Spring HATEOAS ships with a representation model class (`PagedModel`/`SlicedModel`) that allows enriching the content of a `Page` or `Slice` instance with the necessary `Page`/`Slice` metadata as well as links to let the clients easily navigate the pages.
1548+
The conversion of a `Page` to a `PagedModel` is done by an implementation of the Spring HATEOAS `RepresentationModelAssembler` interface, called the `PagedResourcesAssembler`.
1549+
Similarly `Slice` instances can be converted to a `SlicedModel` using a `SlicedResourcesAssembler`.
1550+
The following example shows how to use a `PagedResourcesAssembler` as a controller method argument, as the `SlicedResourcesAssembler` works exactly the same:
15501551

15511552
.Using a PagedResourcesAssembler as controller method argument
15521553
====
@@ -1555,36 +1556,38 @@ The following example shows how to use a `PagedResourcesAssembler` as a controll
15551556
@Controller
15561557
class PersonController {
15571558
1558-
@Autowired PersonRepository repository;
1559+
private final PersonRepository repository;
1560+
1561+
// Constructor omitted
15591562
1560-
@RequestMapping(value = "/persons", method = RequestMethod.GET)
1561-
HttpEntity<PagedResources<Person>> persons(Pageable pageable,
1563+
@GetMapping("/people")
1564+
HttpEntity<PagedModel<Person>> people(Pageable pageable,
15621565
PagedResourcesAssembler assembler) {
15631566
1564-
Page<Person> persons = repository.findAll(pageable);
1565-
return new ResponseEntity<>(assembler.toResources(persons), HttpStatus.OK);
1567+
Page<Person> people = repository.findAll(pageable);
1568+
return ResponseEntity.ok(assembler.toModel(people));
15661569
}
15671570
}
15681571
----
15691572
====
15701573

15711574
Enabling the configuration, as shown in the preceding example, lets the `PagedResourcesAssembler` be used as a controller method argument.
1572-
Calling `toResources(…)` on it has the following effects:
1575+
Calling `toModel(…)` on it has the following effects:
15731576

1574-
* The content of the `Page` becomes the content of the `PagedResources` instance.
1575-
* The `PagedResources` object gets a `PageMetadata` instance attached, and it is populated with information from the `Page` and the underlying `PageRequest`.
1576-
* The `PagedResources` may get `prev` and `next` links attached, depending on the page's state.
1577+
* The content of the `Page` becomes the content of the `PagedModel` instance.
1578+
* The `PagedModel` object gets a `PageMetadata` instance attached, and it is populated with information from the `Page` and the underlying `Pageable`.
1579+
* The `PagedModel` may get `prev` and `next` links attached, depending on the page's state.
15771580
The links point to the URI to which the method maps.
15781581
The pagination parameters added to the method match the setup of the `PageableHandlerMethodArgumentResolver` to make sure the links can be resolved later.
15791582

15801583
Assume we have 30 `Person` instances in the database.
1581-
You can now trigger a request (`GET http://localhost:8080/persons`) and see output similar to the following:
1584+
You can now trigger a request (`GET http://localhost:8080/people`) and see output similar to the following:
15821585

15831586
====
15841587
[source,javascript]
15851588
----
1586-
{ "links" : [ { "rel" : "next",
1587-
"href" : "http://localhost:8080/persons?page=1&size=20" }
1589+
{ "links" : [
1590+
{ "rel" : "next", "href" : "http://localhost:8080/persons?page=1&size=20" }
15881591
],
15891592
"content" : [
15901593
… // 20 Person instances rendered here
@@ -1599,9 +1602,14 @@ You can now trigger a request (`GET http://localhost:8080/persons`) and see outp
15991602
----
16001603
====
16011604

1605+
WARNING: The JSON envelope format shown here doesn't follow any formally specified structure and it's not guaranteed stable and we might change it at any time.
1606+
It's highly recommended to enable the rendering as a hypermedia-enabled, official media type, supported by Spring HATEOAS, like https://docs.spring.io/spring-hateoas/docs/{springHateoasVersion}/reference/html/#mediatypes.hal[HAL].
1607+
Those can be activated by using its `@EnableHypermediaSupport` annotation.
1608+
Find more information in the https://docs.spring.io/spring-hateoas/docs/{springHateoasVersion}/reference/html/#configuration.at-enable[Spring HATEOAS reference documentation].
1609+
16021610
The assembler produced the correct URI and also picked up the default configuration to resolve the parameters into a `Pageable` for an upcoming request.
16031611
This means that, if you change that configuration, the links automatically adhere to the change.
1604-
By default, the assembler points to the controller method it was invoked in, but you can customize that by passing a custom `Link` to be used as base to build the pagination links, which overloads the `PagedResourcesAssembler.toResource(…)` method.
1612+
By default, the assembler points to the controller method it was invoked in, but you can customize that by passing a custom `Link` to be used as base to build the pagination links, which overloads the `PagedResourcesAssembler.toModel(…)` method.
16051613

16061614
[[core.web.basic.jackson-mappers]]
16071615
==== Spring Data Jackson Modules

0 commit comments

Comments
 (0)