diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java index 7be870f39..059ab56d0 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -15,11 +15,13 @@ */ package org.springframework.data.rest.core.config; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.data.rest.core.mapping.ResourceMetadata; import org.springframework.data.rest.core.projection.ProjectionDefinitions; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -38,8 +40,32 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions private final Map> projectionDefinitions; private String parameterName = DEFAULT_PROJECTION_PARAMETER_NAME; + /** + * Creates a new {@link ProjectionDefinitionConfiguration}. + */ public ProjectionDefinitionConfiguration() { + this(Collections. emptySet()); + } + + /** + * Creates a new {@link ProjectionDefinitionConfiguration} from the given {@link ResourceMetadata} instances. + * + * @param resourceMetadata must not be {@literal null}. + */ + public ProjectionDefinitionConfiguration(Iterable resourceMetadata) { + + Assert.notNull(resourceMetadata, "ResourceMetadata must not be null!"); + this.projectionDefinitions = new HashMap>(); + + for (ResourceMetadata metadata : resourceMetadata) { + + Class projection = metadata.getExcerptProjection(); + + if (projection != null) { + addProjection(projection); + } + } } /* @@ -48,7 +74,7 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions */ public String getParameterName() { return parameterName; - }; + } /** * Configures the request parameter name to be used to accept the projection name to be returned. diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfigurationUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfigurationUnitTests.java index 1868ace77..56784fbd8 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfigurationUnitTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/config/ProjectionDefinitionConfigurationUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -17,9 +17,14 @@ package org.springframework.data.rest.core.config; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import java.util.Arrays; + +import org.hamcrest.Matchers; import org.junit.Test; import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration.ProjectionDefinitionKey; +import org.springframework.data.rest.core.mapping.ResourceMetadata; /** * Unit tests for {@link ProjectionDefinitionConfiguration}. @@ -150,11 +155,22 @@ public class ProjectionDefinitionConfigurationUnitTests { is(typeCompatibleWith(ParentProjection.class))); } - @Projection(name = "name", types = Integer.class) - interface SampleProjection { + /** + * @see DATAREST-577 + */ + @Test + public void registersExcerptProjectionsByDefault() { + ResourceMetadata metadata = mock(ResourceMetadata.class); + doReturn(SampleProjection.class).when(metadata).getExcerptProjection(); + + assertThat(new ProjectionDefinitionConfiguration(Arrays.asList(metadata)).getProjectionsFor(Integer.class), + Matchers.> hasEntry("name", SampleProjection.class)); } + @Projection(name = "name", types = Integer.class) + interface SampleProjection {} + @Projection(types = Integer.class) interface Default { diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index c2925979d..ff61cc416 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -217,8 +217,9 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon @Bean public RepositoryRestConfiguration config() { - ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration(); + ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration(resourceMappings()); + // Register projections found in packages for (Class projection : getProjections(repositories())) { configuration.addProjection(projection); }