forked from spring-projects/spring-data-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpringDataJacksonConfiguration.java
159 lines (131 loc) · 5.39 KB
/
SpringDataJacksonConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.web.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.geo.GeoModule;
import org.springframework.data.web.PagedModel;
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializerBase;
import com.fasterxml.jackson.databind.util.StdConverter;
/**
* JavaConfig class to export Jackson specific configuration.
*
* @author Oliver Gierke
* @author Yanming Zhou
*/
public class SpringDataJacksonConfiguration implements SpringDataJacksonModules {
@Nullable
@Autowired(required = false) SpringDataWebSettings settings;
@Bean
public GeoModule jacksonGeoModule() {
return new GeoModule();
}
@Bean
public PageModule pageModule() {
return new PageModule(settings);
}
/**
* A Jackson module customizing the serialization of {@link PageImpl} instances depending on the
* {@link SpringDataWebSettings} handed into the instance. In case of
* {@link org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode#DIRECT} being
* configured, a no-op {@link StdConverter} is registered to issue a one-time warning about the mode being used (as
* it's not recommended).
* {@link org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode#VIA_DTO} would register
* a converter wrapping {@link PageImpl} instances into {@link PagedModel}.
*
* @author Oliver Drotbohm
*/
public static class PageModule extends SimpleModule {
private static final long serialVersionUID = 275254460581626332L;
private static final String UNPAGED_TYPE_NAME = "org.springframework.data.domain.Unpaged";
private static final Class<?> UNPAGED_TYPE;
static {
UNPAGED_TYPE = ClassUtils.resolveClassName(UNPAGED_TYPE_NAME, PageModule.class.getClassLoader());
}
/**
* Creates a new {@link PageModule} for the given {@link SpringDataWebSettings}.
*
* @param settings can be {@literal null}.
*/
public PageModule(@Nullable SpringDataWebSettings settings) {
addSerializer(UNPAGED_TYPE, new UnpagedAsInstanceSerializer());
if (settings == null || settings.pageSerializationMode() == PageSerializationMode.DIRECT) {
setMixInAnnotation(PageImpl.class, WarningMixing.class);
} else {
setMixInAnnotation(PageImpl.class, WrappingMixing.class);
}
}
/**
* A Jackson serializer rendering instances of {@link org.springframework.data.domain.Unpaged} as {@code INSTANCE}
* as it was previous rendered.
*
* @author Oliver Drotbohm
*/
static class UnpagedAsInstanceSerializer extends ToStringSerializerBase {
private static final long serialVersionUID = -1213451755610144637L;
public UnpagedAsInstanceSerializer() {
super(Object.class);
}
@Override
public String valueToString(@Nullable Object value) {
return "INSTANCE";
}
}
/**
* A mixin for PageImpl to register a converter issuing the serialization warning.
*
* @author Oliver Drotbohm
*/
@JsonSerialize(converter = PlainPageSerializationWarning.class)
abstract class WarningMixing {}
@JsonSerialize(converter = PageModelConverter.class)
abstract class WrappingMixing {}
static class PageModelConverter extends StdConverter<Page<?>, PagedModel<?>> {
@Nullable
@Override
public PagedModel<?> convert(@Nullable Page<?> value) {
return value == null ? null : new PagedModel<>(value);
}
}
static class PlainPageSerializationWarning extends StdConverter<Page<?>, Page<?>> {
private static final Logger LOGGER = LoggerFactory.getLogger(PlainPageSerializationWarning.class);
private static final String MESSAGE = """
Serializing PageImpl instances as-is is not supported, meaning that there is no guarantee about the stability of the resulting JSON structure!
For a stable JSON structure, please use Spring Data's PagedModel (globally via @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO))
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.
""";
private boolean warningRendered = false;
@Nullable
@Override
public Page<?> convert(@Nullable Page<?> value) {
if (!warningRendered) {
this.warningRendered = true;
LOGGER.warn(MESSAGE);
}
return value;
}
}
}
}