DATAREST-925 - Invoke ResourceProcessors for ProjectionResources.

The serializer for projection resources now also invokes ResourceProcessor instances registered for that particular projection.

Original pull request: #238.
This commit is contained in:
Craig Andrews
2016-11-03 13:16:34 -04:00
committed by Oliver Gierke
parent 080d1b0dab
commit b66c8272ee
9 changed files with 94 additions and 25 deletions

View File

@@ -97,7 +97,6 @@ import org.springframework.data.rest.webmvc.json.MappingAwarePageableArgumentRes
import org.springframework.data.rest.webmvc.json.MappingAwareSortArgumentResolver;
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module;
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.LookupObjectSerializer;
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.NestedEntitySerializer;
import org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter;
import org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter.ValueTypeSchemaPropertyCustomizerFactory;
import org.springframework.data.rest.webmvc.mapping.Associations;
@@ -637,12 +636,11 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
EmbeddedResourcesAssembler assembler = new EmbeddedResourcesAssembler(entities, associationLinks(),
excerptProjector());
NestedEntitySerializer serializer = new NestedEntitySerializer(entities, assembler, resourceProcessorInvoker());
LookupObjectSerializer lookupObjectSerializer = new LookupObjectSerializer(
OrderAwarePluginRegistry.create(getEntityLookups()));
return new PersistentEntityJackson2Module(associationLinks(), entities, uriToEntityConverter, linkCollector(),
repositoryInvokerFactory, serializer, lookupObjectSerializer);
repositoryInvokerFactory, lookupObjectSerializer, resourceProcessorInvoker(), assembler);
}
@Bean

View File

@@ -116,7 +116,8 @@ public class PersistentEntityJackson2Module extends SimpleModule {
*/
public PersistentEntityJackson2Module(Associations associations, PersistentEntities entities,
UriToEntityConverter converter, LinkCollector collector, RepositoryInvokerFactory factory,
NestedEntitySerializer serializer, LookupObjectSerializer lookupObjectSerializer) {
LookupObjectSerializer lookupObjectSerializer, ResourceProcessorInvoker resourceProcessorInvoker,
EmbeddedResourcesAssembler assembler) {
super(new Version(2, 0, 0, null, "org.springframework.data.rest", "jackson-module"));
@@ -125,8 +126,9 @@ public class PersistentEntityJackson2Module extends SimpleModule {
Assert.notNull(converter, "UriToEntityConverter must not be null!");
Assert.notNull(collector, "LinkCollector must not be null!");
NestedEntitySerializer serializer = new NestedEntitySerializer(entities, assembler, resourceProcessorInvoker);
addSerializer(new PersistentEntityResourceSerializer(collector));
addSerializer(new ProjectionSerializer(collector, associations, false));
addSerializer(new ProjectionSerializer(collector, associations, resourceProcessorInvoker, false));
addSerializer(new ProjectionResourceContentSerializer(false));
setSerializerModifier(
@@ -321,7 +323,7 @@ public class PersistentEntityJackson2Module extends SimpleModule {
* @author Alex Leigh
* @since 2.5
*/
public static class NestedEntitySerializer extends StdSerializer<Object> {
static class NestedEntitySerializer extends StdSerializer<Object> {
private static final long serialVersionUID = -2327469118972125954L;
@@ -538,6 +540,7 @@ public class PersistentEntityJackson2Module extends SimpleModule {
private final LinkCollector collector;
private final Associations associations;
private final ResourceProcessorInvoker resourceProcessorInvoker;
private final boolean unwrapping;
/**
@@ -548,12 +551,13 @@ public class PersistentEntityJackson2Module extends SimpleModule {
* @param mappings must not be {@literal null}.
* @param unwrapping
*/
private ProjectionSerializer(LinkCollector collector, Associations mappings, boolean unwrapping) {
private ProjectionSerializer(LinkCollector collector, Associations mappings, ResourceProcessorInvoker resourceProcessorInvoker, boolean unwrapping) {
super(TargetAware.class);
this.collector = collector;
this.associations = mappings;
this.resourceProcessorInvoker = resourceProcessorInvoker;
this.unwrapping = unwrapping;
}
@@ -565,10 +569,6 @@ public class PersistentEntityJackson2Module extends SimpleModule {
public void serialize(TargetAware value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
Object target = value.getTarget();
Links links = associations.getMetadataFor(value.getTargetClass()).isExported() ? collector.getLinksFor(target)
: new Links();
if (!unwrapping) {
jgen.writeStartObject();
}
@@ -576,13 +576,21 @@ public class PersistentEntityJackson2Module extends SimpleModule {
provider.//
findValueSerializer(ProjectionResource.class, null).//
unwrappingSerializer(null).//
serialize(new ProjectionResource(value, links), jgen, provider);
serialize(toResource(value), jgen, provider);
if (!unwrapping) {
jgen.writeEndObject();
}
}
private ProjectionResource toResource(TargetAware value) {
Object target = value.getTarget();
Links links = associations.getMetadataFor(value.getTargetClass()).isExported() ? collector.getLinksFor(target)
: new Links();
Resource<TargetAware> resource = resourceProcessorInvoker.invokeProcessorsFor(new Resource<TargetAware>(value, links));
return new ProjectionResource(resource.getContent(), resource.getLinks());
}
/*
* (non-Javadoc)
* @see com.fasterxml.jackson.databind.JsonSerializer#isUnwrappingSerializer()
@@ -598,7 +606,7 @@ public class PersistentEntityJackson2Module extends SimpleModule {
*/
@Override
public JsonSerializer<TargetAware> unwrappingSerializer(NameTransformer unwrapper) {
return new ProjectionSerializer(collector, associations, true);
return new ProjectionSerializer(collector, associations, resourceProcessorInvoker, true);
}
}