GH-2987 - Fix broken serialization of Unpaged instances.
We now reinstantiate the serialization of Unpaged instances as plain INSTANCE strings as they were rendered before Unpaged was changed from an enum into a class. Note, that we still strongly advise, not to serialize Page instances directly as it's a domain type, its Jackson-level surface is subject to change if we need to change the type's API for unrelated reasons.
This commit is contained in:
@@ -15,8 +15,19 @@
|
||||
*/
|
||||
package org.springframework.data.web.config;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.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.
|
||||
@@ -29,4 +40,45 @@ public class SpringDataJacksonConfiguration implements SpringDataJacksonModules
|
||||
public GeoModule jacksonGeoModule() {
|
||||
return new GeoModule();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PageModule pageModule() {
|
||||
return new PageModule();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
public PageModule() {
|
||||
addSerializer(UNPAGED_TYPE, new UnpagedAsInstanceSerializer());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user