Skip to content

Commit 4ebe936

Browse files
committed
Issue a warning log when serializing PageImpl to JSON directly.
We now issue a one-time warning log if Jackson gets handed a PageImpl to serialize as the Jackson structure might need to change for arbitrary other reasons. Fixes GH-2987.
1 parent 3f14df6 commit 4ebe936

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java

+33
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public static class PageModule extends SimpleModule {
5858
}
5959

6060
public PageModule() {
61+
6162
addSerializer(UNPAGED_TYPE, new UnpagedAsInstanceSerializer());
63+
setMixInAnnotation(PageImpl.class, PageImplMixin.class);
6264
}
6365

6466
/**
@@ -80,5 +82,36 @@ public String valueToString(@Nullable Object value) {
8082
return "INSTANCE";
8183
}
8284
}
85+
86+
/**
87+
* A mixin for PageImpl to register a converter issuing the serialization warning.
88+
*
89+
* @author Oliver Drotbohm
90+
*/
91+
@JsonSerialize(converter = PlainPageSerializationWarning.class)
92+
abstract class PageImplMixin {}
93+
94+
static class PlainPageSerializationWarning extends StdConverter<Page<?>, Page<?>> {
95+
96+
private static final Logger LOGGER = LoggerFactory.getLogger(PlainPageSerializationWarning.class);
97+
private static final String MESSAGE = """
98+
Serializing PageImpl instances as-is is not supported, meaning that there is no guarantee about the stability of the resulting JSON structure!
99+
For a stable JSON structure, please use 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.
100+
""";
101+
102+
private boolean warningRendered = false;
103+
104+
@Nullable
105+
@Override
106+
public Page<?> convert(@Nullable Page<?> value) {
107+
108+
if (!warningRendered) {
109+
this.warningRendered = true;
110+
LOGGER.warn(MESSAGE);
111+
}
112+
113+
return value;
114+
}
115+
}
83116
}
84117
}

0 commit comments

Comments
 (0)