From e79b73664ace9e0032779b8d0db091b54e5d83b8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 12 Mar 2018 11:51:53 +0100 Subject: [PATCH] DATAREST-1212 - Deprecated RepositoryRestConfigurerAdapter. We now recommend to implement RepositoryRestConfigurer directly as its declared methods are now default methods. We also introduced a static factory method to easily create a configurer to customize RepositoryRestConfiguration via a Lambda expression. Moved test case setups that use that deprecated API to the new one. --- ...itoryEntityControllerIntegrationTests.java | 9 ++--- .../alps/AlpsControllerIntegrationTests.java | 10 ++--- .../rest/webmvc/jpa/CorsIntegrationTests.java | 16 +++----- .../rest/webmvc/jpa/DataRest363Tests.java | 15 +++---- .../jpa/JpaDefaultPageableWebTests.java | 4 +- .../webmvc/jpa/ProfileIntegrationTests.java | 4 +- .../config/RepositoryRestConfigurer.java | 39 ++++++++++++++++--- .../RepositoryRestConfigurerAdapter.java | 2 + ...ryRestMvConfigurationIntegrationTests.java | 34 ++++++++-------- 9 files changed, 79 insertions(+), 54 deletions(-) diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java index 492839da8..47dd137c1 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java @@ -157,11 +157,10 @@ public class RepositoryEntityControllerIntegrationTests extends AbstractControll List value = entity.getHeaders().get("Accept-Patch"); assertThat(value).hasSize(3); - assertThat(value, - hasItems(// - RestMediaTypes.JSON_PATCH_JSON.toString(), // - RestMediaTypes.MERGE_PATCH_JSON.toString(), // - MediaType.APPLICATION_JSON_VALUE)); + assertThat(value, hasItems(// + RestMediaTypes.JSON_PATCH_JSON.toString(), // + RestMediaTypes.MERGE_PATCH_JSON.toString(), // + MediaType.APPLICATION_JSON_VALUE)); } @Test // DATAREST-34 diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java index 5da1287e6..13ce55836 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java @@ -34,7 +34,7 @@ import org.springframework.data.rest.tests.AbstractControllerIntegrationTests; import org.springframework.data.rest.tests.TestMvcClient; import org.springframework.data.rest.webmvc.ProfileController; import org.springframework.data.rest.webmvc.RestMediaTypes; -import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; import org.springframework.data.rest.webmvc.jpa.Item; import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig; import org.springframework.hateoas.Link; @@ -65,7 +65,7 @@ public class AlpsControllerIntegrationTests extends AbstractControllerIntegratio @Autowired RepositoryRestConfiguration configuration; @Configuration - static class Config extends RepositoryRestConfigurerAdapter { + static class Config { @Bean public LinkDiscoverer alpsLinkDiscoverer() { @@ -73,9 +73,9 @@ public class AlpsControllerIntegrationTests extends AbstractControllerIntegratio MediaType.valueOf("application/alps+json")); } - @Override - public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { - config.exposeIdsFor(Item.class); + @Bean + RepositoryRestConfigurer configurer() { + return RepositoryRestConfigurer.withConfig(config -> config.exposeIdsFor(Item.class)); } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java index 220a659bd..f805fd468 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java @@ -21,12 +21,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import org.junit.Test; import org.springframework.context.annotation.Bean; -import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.tests.AbstractWebIntegrationTests; import org.springframework.data.rest.webmvc.BasePathAwareController; import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; -import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; import org.springframework.hateoas.Link; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -51,16 +49,12 @@ public class CorsIntegrationTests extends AbstractWebIntegrationTests { @Bean RepositoryRestConfigurer repositoryRestConfigurer() { - return new RepositoryRestConfigurerAdapter() { + return RepositoryRestConfigurer.withConfig(config -> { - @Override - public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { - - config.getCorsRegistry().addMapping("/books/**") // - .allowedMethods("GET", "PUT", "POST") // - .allowedOrigins("http://far.far.away"); - } - }; + config.getCorsRegistry().addMapping("/books/**") // + .allowedMethods("GET", "PUT", "POST") // + .allowedOrigins("http://far.far.away"); + }); } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest363Tests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest363Tests.java index 820cf6123..f0fb00e72 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest363Tests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest363Tests.java @@ -25,9 +25,8 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.tests.TestMvcClient; -import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; import org.springframework.hateoas.LinkDiscoverer; import org.springframework.hateoas.LinkDiscoverers; @@ -38,6 +37,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @@ -64,16 +64,17 @@ public class DataRest363Tests { Person frodo; @Configuration - static class Config extends RepositoryRestConfigurerAdapter { + static class Config { @Bean - public LinkDiscoverer classicLinkDiscover() { + LinkDiscoverer classicLinkDiscover() { return new JsonPathLinkDiscoverer("$.links[?(@.rel == '%s')].href", MEDIA_TYPE); } - @Override - public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { - config.setDefaultMediaType(MEDIA_TYPE).useHalAsDefaultJsonMediaType(false); + @Bean + RepositoryRestConfigurer configurer() { + return RepositoryRestConfigurer.withConfig( // + it -> it.setDefaultMediaType(MEDIA_TYPE).useHalAsDefaultJsonMediaType(false)); } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java index 688dac9ba..7d1ed0732 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaDefaultPageableWebTests.java @@ -35,7 +35,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.tests.AbstractWebIntegrationTests; import org.springframework.data.rest.webmvc.RepositoryRestController; -import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; import org.springframework.hateoas.Link; import org.springframework.test.context.ContextConfiguration; @@ -58,7 +58,7 @@ public class JpaDefaultPageableWebTests extends AbstractWebIntegrationTests { @Configuration @Import({ RepositoryRestMvcConfiguration.class, JpaRepositoryConfig.class }) @EnableJpaRepositories(considerNestedRepositories = true) - static class Config extends RepositoryRestConfigurerAdapter { + static class Config implements RepositoryRestConfigurer { public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.setDefaultPageSize(1); diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/ProfileIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/ProfileIntegrationTests.java index f3a2f7772..561b0cbfa 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/ProfileIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/ProfileIntegrationTests.java @@ -29,7 +29,7 @@ import org.springframework.data.rest.tests.TestMvcClient; import org.springframework.data.rest.webmvc.ProfileController; import org.springframework.data.rest.webmvc.ProfileResourceProcessor; import org.springframework.data.rest.webmvc.RestMediaTypes; -import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkDiscoverers; import org.springframework.http.MediaType; @@ -56,7 +56,7 @@ public class ProfileIntegrationTests extends AbstractControllerIntegrationTests private static final String ROOT_URI = "/api"; @Configuration - static class Config extends RepositoryRestConfigurerAdapter { + static class Config implements RepositoryRestConfigurer { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurer.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurer.java index a3fd6ee8d..98ccc6117 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurer.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurer.java @@ -16,11 +16,13 @@ package org.springframework.data.rest.webmvc.config; import java.util.List; +import java.util.function.Consumer; import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.util.Assert; import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver; import com.fasterxml.jackson.databind.ObjectMapper; @@ -34,19 +36,44 @@ import com.fasterxml.jackson.databind.ObjectMapper; */ public interface RepositoryRestConfigurer { + /** + * Convenience method to easily create simple {@link RepositoryRestConfigurer} instances that solely want to tweak the + * {@link RepositoryRestConfiguration}. + * + * @param consumer must not be {@literal null}. + * @return + * @since 3.1 + */ + static RepositoryRestConfigurer withConfig(Consumer consumer) { + + Assert.notNull(consumer, "Consumer must not be null!"); + + return new RepositoryRestConfigurer() { + + /* + * (non-Javadoc) + * @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureRepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration) + */ + @Override + public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { + consumer.accept(config); + } + }; + } + /** * Override this method to add additional configuration. * * @param config Main configuration bean. */ - void configureRepositoryRestConfiguration(RepositoryRestConfiguration config); + default void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {} /** * Override this method to add your own converters. * * @param conversionService Default ConversionService bean. */ - void configureConversionService(ConfigurableConversionService conversionService); + default void configureConversionService(ConfigurableConversionService conversionService) {} /** * Override this method to add validators manually. @@ -54,26 +81,26 @@ public interface RepositoryRestConfigurer { * @param validatingListener The {@link org.springframework.context.ApplicationListener} responsible for invoking * {@link org.springframework.validation.Validator} instances. */ - void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener); + default void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {} /** * Configure the {@link ExceptionHandlerExceptionResolver}. * * @param exceptionResolver The default exception resolver on which you can add custom argument resolvers. */ - void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver); + default void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver) {} /** * Configure the available {@link HttpMessageConverter}s by adding your own. * * @param messageConverters The converters to be used by the system. */ - void configureHttpMessageConverters(List> messageConverters); + default void configureHttpMessageConverters(List> messageConverters) {} /** * Configure the Jackson {@link ObjectMapper} directly. * * @param objectMapper The {@literal ObjectMapper} to be used by the system. */ - void configureJacksonObjectMapper(ObjectMapper objectMapper); + default void configureJacksonObjectMapper(ObjectMapper objectMapper) {} } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurerAdapter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurerAdapter.java index 0aecdd72a..28c706a73 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurerAdapter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurerAdapter.java @@ -31,7 +31,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; * @author Oliver Gierke * @since 2.4 * @soundtrack Florian Reichelt & Max Ender - Abschlusskonzert (https://www.youtube.com/watch?v=5WP0P-ndinY) + * @deprecated since 3.1, implement {@link RepositoryRestConfigurer} directly. */ +@Deprecated public class RepositoryRestConfigurerAdapter implements RepositoryRestConfigurer { /* diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java index d850e5fdc..f63a0e6d7 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java @@ -39,7 +39,6 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.geo.Distance; import org.springframework.data.geo.Point; -import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.webmvc.RepositoryLinksResource; import org.springframework.data.rest.webmvc.RestMediaTypes; import org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter; @@ -197,41 +196,44 @@ public class RepositoryRestMvConfigurationIntegrationTests { @Configuration @Import(RepositoryRestMvcConfiguration.class) - static class ExtendingConfiguration extends RepositoryRestConfigurerAdapter { + static class ExtendingConfiguration { @Bean - public DefaultRelProvider relProvider() { + DefaultRelProvider relProvider() { return new DefaultRelProvider(); } @Bean - public CollectingComponent collectingComponent() { + CollectingComponent collectingComponent() { return new CollectingComponent(); } - @Override - public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { + @Bean + RepositoryRestConfigurer configurer() { - config.setDefaultPageSize(45); - config.setMaxPageSize(7000); - config.setPageParamName("myPage"); - config.setLimitParamName("mySize"); - config.setSortParamName("mySort"); + return RepositoryRestConfigurer.withConfig(config -> { + + config.setDefaultPageSize(45); + config.setMaxPageSize(7000); + config.setPageParamName("myPage"); + config.setLimitParamName("mySize"); + config.setSortParamName("mySort"); + }); } } @Configuration @Import(RepositoryRestMvcConfiguration.class) - static class NonHalConfiguration extends RepositoryRestConfigurerAdapter { + static class NonHalConfiguration { @Bean - public CollectingComponent collectingComponent() { + CollectingComponent collectingComponent() { return new CollectingComponent(); } - @Override - public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { - config.useHalAsDefaultJsonMediaType(false); + @Bean + RepositoryRestConfigurer configurer() { + return RepositoryRestConfigurer.withConfig(config -> config.useHalAsDefaultJsonMediaType(false)); } }