Skip to content

Commit a21a179

Browse files
committed
Support for simplified rendering of Page instances via PagedModel.
This commits all necessary infrastructure to produce simplified JSON representation rendering for Page instances to make sure the representations stay stable and do not expose unnecessary implementation details. The support consists of the following elements: - PagedModel, a stripped down variant of the Spring HATEOAS counterpart to produce an equivalent JSON representation but without the hypermedia elements. This allows a gradual migration to Spring HATEOAS if needed. Page instances can be wrapped into PagedModel once and returned from controller methods to create the new, simplified JSON representation. - @EnableSpringDataWeb support now contains a pageSerializationMode attribute set to an enum with two possible values: DIRECT, which is the default for backwards compatibility reasons. It serializes Page instances directly but issues a warning that either the newly introduced support here or the Spring HATEOAS support should be used to avoid accidentally breaking representations. The other value, VIA_DTO causes all PageImpl instances to be rendered being wrapped in a PagedModel automatically by registering a Jackson StdConverter applying the wrapping transparently. Internally, the configuration of @EnableSpringDataWebSupport is translated into a bean definition of a newly introduced type SpringDataWebSettings and wired into the web configuration for consideration within a Jackson module, customizing the serialization for PageImpl. Fixes GH-3024.
1 parent 03710fc commit a21a179

File tree

7 files changed

+359
-13
lines changed

7 files changed

+359
-13
lines changed

src/main/antora/modules/ROOT/pages/repositories/core-extensions.adoc

+86-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,92 @@ You have to populate `thing1_page`, `thing2_page`, and so on.
189189

190190
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.
191191

192+
[[core.web.page]]
193+
=== Creating JSON representations for `Page`
194+
195+
It's common for Spring MVC controllers to try to ultimately render a representation of a Spring Data page to clients.
196+
While one could simply return `Page` instances from handler methods to let Jackson render them as is, we strongly recommend against this as the underlying implementation class `PageImpl` is a domain type.
197+
This means we might want or have to change its API for unrelated reasons, and such changes might alter the resulting JSON representation in a breaking way.
198+
199+
With Spring Data 3.1, we started hinting at the problem by issuing a warning log describing the problem.
200+
We still ultimately recommend to leverage xref:repositories/core-extensions.adoc#core.web.pageables[the integration with Spring HATEOAS] for a fully stable and hypermedia-enabled way of rendering pages that easily allow clients to navigate them.
201+
But as of version 3.3 Spring Data ships a page rendering mechanism that is convenient to use but does not require the inclusion of Spring HATEOAS.
202+
203+
[[core.web.page.paged-model]]
204+
==== Using Spring Data' `PagedModel`
205+
206+
At its core, the support consists of a simplified version of Spring HATEOAS' `PagedModel` (the Spring Data one located in the `org.springframework.data.web` package).
207+
It can be used to wrap `Page` instances and result in a simplified representation that reflects the structure established by Spring HATEOAS but omits the navigation links.
208+
209+
[source, java]
210+
----
211+
import org.springframework.data.web.PagedModel;
212+
213+
@Controller
214+
class MyController {
215+
216+
private final MyRepository repository;
217+
218+
// Constructor ommitted
219+
220+
@GetMapping("/page")
221+
PagedModel<?> page(Pageable pageable) {
222+
return new PagedModel<>(repository.findAll(pageable)); // <1>
223+
}
224+
}
225+
----
226+
<1> Wraps the `Page` instance into a `PagedModel`.
227+
228+
This will result in a JSON structure looking like this:
229+
230+
[source, javascript]
231+
----
232+
{
233+
"content" : [
234+
… // Page content rendered here
235+
],
236+
"page" : {
237+
"size" : 20,
238+
"totalElements" : 30,
239+
"totalPages" : 2,
240+
"number" : 0
241+
}
242+
}
243+
----
244+
245+
Note how the document contains a `page` field exposing the essential pagination metadata.
246+
247+
[[core.web.page.config]]
248+
==== Globally enabling simplified `Page` rendering
249+
250+
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:
251+
252+
[source, java]
253+
----
254+
@EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO)
255+
class MyConfiguration { }
256+
----
257+
258+
This will allow your controller to still return `Page` instances and they will automatically be rendered into the simplified representation:
259+
260+
[source, java]
261+
----
262+
@Controller
263+
class MyController {
264+
265+
private final MyRepository repository;
266+
267+
// Constructor ommitted
268+
269+
@GetMapping("/page")
270+
Page<?> page(Pageable pageable) {
271+
return repository.findAll(pageable);
272+
}
273+
}
274+
----
275+
192276
[[core.web.pageables]]
193-
=== Hypermedia Support for `Page` and `Slice`
277+
==== Hypermedia Support for `Page` and `Slice`
194278

195279
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.
196280
The conversion of a `Page` to a `PagedModel` is done by an implementation of the Spring HATEOAS `RepresentationModelAssembler` interface, called the `PagedResourcesAssembler`.
@@ -237,7 +321,7 @@ You can now trigger a request (`GET http://localhost:8080/people`) and see outpu
237321
"content" : [
238322
… // 20 Person instances rendered here
239323
],
240-
"pageMetadata" : {
324+
"page" : {
241325
"size" : 20,
242326
"totalElements" : 30,
243327
"totalPages" : 2,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.web;
17+
18+
import java.util.List;
19+
import java.util.Objects;
20+
21+
import org.springframework.data.domain.Page;
22+
import org.springframework.lang.Nullable;
23+
import org.springframework.util.Assert;
24+
25+
import com.fasterxml.jackson.annotation.JsonProperty;
26+
27+
/**
28+
* DTO to build stable JSON representations of a Spring Data {@link Page}. It can either be selectively used in
29+
* controller methods by calling {@code new PagedModel<>(page)} or generally activated as representation model for
30+
* {@link org.springframework.data.domain.PageImpl} instances by setting
31+
* {@link org.springframework.data.web.config.EnableSpringDataWebSupport}'s {@code pageSerializationMode} to
32+
* {@link org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode#VIA_DTO}.
33+
*
34+
* @author Oliver Drotbohm
35+
* @author Greg Turnquist
36+
* @since 3.3
37+
*/
38+
public class PagedModel<T> {
39+
40+
private final Page<T> page;
41+
42+
/**
43+
* Creates a new {@link PagedModel} for the given {@link Page}.
44+
*
45+
* @param page must not be {@literal null}.
46+
*/
47+
public PagedModel(Page<T> page) {
48+
49+
Assert.notNull(page, "Page must not be null");
50+
51+
this.page = page;
52+
}
53+
54+
@JsonProperty
55+
public List<T> getContent() {
56+
return page.getContent();
57+
}
58+
59+
@Nullable
60+
@JsonProperty("page")
61+
public PageMetadata getMetadata() {
62+
return new PageMetadata(page.getSize(), page.getNumber(), page.getTotalElements(),
63+
page.getTotalPages());
64+
}
65+
66+
@Override
67+
public boolean equals(@Nullable Object obj) {
68+
69+
if (this == obj) {
70+
return true;
71+
}
72+
73+
if (!(obj instanceof PagedModel<?> that)) {
74+
return false;
75+
}
76+
77+
return Objects.equals(this.page, that.page);
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hash(page);
83+
}
84+
85+
public static record PageMetadata(long size, long number, long totalElements, long totalPages) {
86+
87+
public PageMetadata {
88+
Assert.isTrue(size > -1, "Size must not be negative!");
89+
Assert.isTrue(number > -1, "Number must not be negative!");
90+
Assert.isTrue(totalElements > -1, "Total elements must not be negative!");
91+
Assert.isTrue(totalPages > -1, "Total pages must not be negative!");
92+
}
93+
}
94+
}

src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java

+75-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@
2222
import java.lang.annotation.Target;
2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.Optional;
2627

28+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
29+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
30+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31+
import org.springframework.beans.factory.support.BeanNameGenerator;
2732
import org.springframework.context.ResourceLoaderAware;
2833
import org.springframework.context.annotation.Import;
34+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
2935
import org.springframework.context.annotation.ImportSelector;
3036
import org.springframework.core.io.ResourceLoader;
3137
import org.springframework.core.io.support.SpringFactoriesLoader;
3238
import org.springframework.core.type.AnnotationMetadata;
3339
import org.springframework.data.querydsl.QuerydslUtils;
34-
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
3540
import org.springframework.util.ClassUtils;
3641

3742
/**
@@ -68,10 +73,42 @@
6873
@Retention(RetentionPolicy.RUNTIME)
6974
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
7075
@Inherited
71-
@Import({ EnableSpringDataWebSupport.SpringDataWebConfigurationImportSelector.class,
72-
EnableSpringDataWebSupport.QuerydslActivator.class })
76+
@Import({
77+
EnableSpringDataWebSupport.SpringDataWebConfigurationImportSelector.class,
78+
EnableSpringDataWebSupport.QuerydslActivator.class,
79+
EnableSpringDataWebSupport.SpringDataWebSettingsRegistar.class
80+
})
7381
public @interface EnableSpringDataWebSupport {
7482

83+
/**
84+
* Configures how to render {@link org.springframework.data.domain.PageImpl} instances. Defaults to
85+
* {@link PageSerializationMode#DIRECT} for backward compatibility reasons. Prefer explicitly setting this to
86+
* {@link PageSerializationMode#VIA_DTO}, or manually convert {@link org.springframework.data.domain.PageImpl}
87+
* instances before handing them out of a controller method, either by manually calling {@code new PagedModel<>(page)}
88+
* or using Spring HATEOAS {@link org.springframework.hateoas.PagedModel} abstraction.
89+
*
90+
* @return will never be {@literal null}.
91+
* @since 3.3
92+
*/
93+
PageSerializationMode pageSerializationMode() default PageSerializationMode.DIRECT;
94+
95+
enum PageSerializationMode {
96+
97+
/**
98+
* {@link org.springframework.data.domain.PageImpl} instances will be rendered as is (discouraged, as there's no
99+
* guarantee on the stability of the serialization result as we might need to change the type's API for unrelated
100+
* reasons).
101+
*/
102+
DIRECT,
103+
104+
/**
105+
* Causes {@link org.springframework.data.domain.PageImpl} instances to be wrapped into
106+
* {@link org.springframework.data.web.PagedModel} instances before rendering them as JSON to make sure the
107+
* representation stays stable even if {@link org.springframework.data.domain.PageImpl} is changed.
108+
*/
109+
VIA_DTO;
110+
}
111+
75112
/**
76113
* Import selector to import the appropriate configuration class depending on whether Spring HATEOAS is present on the
77114
* classpath. We need to register the HATEOAS specific class first as apparently only the first class implementing
@@ -127,4 +164,39 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
127164
: new String[0];
128165
}
129166
}
167+
168+
/**
169+
* Registers a bean definition for {@link SpringDataWebSettings} carrying the configuration values of
170+
* {@link EnableSpringDataWebSupport}.
171+
*
172+
* @author Oliver Drotbohm
173+
* @soundtrack Norah Jones - Chasing Pirates
174+
* @since 3.3
175+
*/
176+
static class SpringDataWebSettingsRegistar implements ImportBeanDefinitionRegistrar {
177+
178+
/*
179+
* (non-Javadoc)
180+
* @see org.springframework.context.annotation.ImportBeanDefinitionRegistrar#registerBeanDefinitions(org.springframework.core.type.AnnotationMetadata, org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.beans.factory.support.BeanNameGenerator)
181+
*/
182+
@Override
183+
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
184+
BeanNameGenerator importBeanNameGenerator) {
185+
186+
Map<String, Object> attributes = importingClassMetadata
187+
.getAnnotationAttributes(EnableSpringDataWebSupport.class.getName());
188+
189+
if (attributes == null) {
190+
return;
191+
}
192+
193+
AbstractBeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(SpringDataWebSettings.class)
194+
.addConstructorArgValue(attributes.get("pageSerializationMode"))
195+
.getBeanDefinition();
196+
197+
String beanName = importBeanNameGenerator.generateBeanName(definition, registry);
198+
199+
registry.registerBeanDefinition(beanName, definition);
200+
}
201+
}
130202
}

0 commit comments

Comments
 (0)