From 24c63c9b55956cc26e4c2517eba7a8426e2073ce Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 21 Jul 2015 16:27:24 +0100 Subject: [PATCH] Adopt RepositoryRestConfigurer and discourage subclassing A RepositoryRestMvcConfiguration subclass provided by a user is problematic in a Spring Boot application as it causes RepositoryRestMvcConfiguration's bean declarations to be processed before any auto-configuration runs. One problem that this causes is that it switches off Boot's Jackson auto-configuration due to RepositoryRestMvcConfiguration having already declared multiple ObjectMapper beans. Unlike Boot's auto-configured ObjectMapper, none of these ObjectMappers are marked as @Primary. This then leads to wiring failures due to multiple candidates being available. To address this problem a new RepositoryRestConfigurer abstract has been introduced in Spring Data Gosling. Its use is now strongly preferred over subclassing RepositoryRestMvcConfiguration. Note that our own RepositoryRestMvcConfiguration subclass remains. It is imported as part of auto-configuration (avoiding the ordering problems described above), and provides configuration properties binding for RepositoryRestConfiguration. However, the Jackson ObjectMapper configuration has been moved out into a new RepositoryRestConfigurer implementation. While SpringBootRepositoryRestMvcConfiguration remains, this commit makes it package private to discourage users from subclassing it. While this may break existing applications, it, coupled with the documentation updates, will hopefully guide them toward using RepositoryRestConfigurer. Closes gh-3439 --- .../RepositoryRestMvcAutoConfiguration.java | 6 +++ .../SpringBootRepositoryRestConfigurer.java | 44 +++++++++++++++++++ ...ingBootRepositoryRestMvcConfiguration.java | 21 +-------- ...positoryRestMvcAutoConfigurationTests.java | 16 ------- spring-boot-docs/src/main/asciidoc/howto.adoc | 11 ++--- 5 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfiguration.java index 49f552048d..349a199005 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfiguration.java @@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; @@ -49,4 +50,9 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio @Import(SpringBootRepositoryRestMvcConfiguration.class) public class RepositoryRestMvcAutoConfiguration { + @Bean + public SpringBootRepositoryRestConfigurer springBootRepositoryRestConfigurer() { + return new SpringBootRepositoryRestConfigurer(); + } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java new file mode 100644 index 0000000000..1c14d1b296 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-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. + * You may obtain a copy of the License at + * + * http://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.boot.autoconfigure.data.rest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * A {@code RepositoryRestConfigurer} that applies our configuration to Spring Data REST. + * Specifically, if a {@link Jackson2ObjectMapperBuilder} is available, it is used to + * configure Spring Data REST's {@link ObjectMapper ObjectMappers}. + * + * @author Andy Wilkinson + */ +class SpringBootRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter { + + @Autowired(required = false) + private Jackson2ObjectMapperBuilder objectMapperBuilder; + + @Override + public void configureJacksonObjectMapper(ObjectMapper objectMapper) { + if (this.objectMapperBuilder != null) { + this.objectMapperBuilder.configure(objectMapper); + } + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.java index 0be878c7a8..a28accba4a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.java @@ -16,32 +16,22 @@ package org.springframework.boot.autoconfigure.data.rest; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; 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.webmvc.config.RepositoryRestMvcConfiguration; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; - -import com.fasterxml.jackson.databind.ObjectMapper; /** * A specialized {@link RepositoryRestMvcConfiguration} that applies configuration items - * from the {@code spring.data.rest} namespace. Also configures Jackson if it's available + * from the {@code spring.data.rest} namespace. *

- * Favor an extension of this class instead of extending directly from - * {@link RepositoryRestMvcConfiguration}. * * @author Stephane Nicoll * @since 1.2.2 */ @Configuration -public class SpringBootRepositoryRestMvcConfiguration extends - RepositoryRestMvcConfiguration { - - @Autowired(required = false) - private Jackson2ObjectMapperBuilder objectMapperBuilder; +class SpringBootRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration { @Bean @ConfigurationProperties(prefix = "spring.data.rest") @@ -50,11 +40,4 @@ public class SpringBootRepositoryRestMvcConfiguration extends return super.config(); } - @Override - protected void configureJacksonObjectMapper(ObjectMapper objectMapper) { - if (this.objectMapperBuilder != null) { - this.objectMapperBuilder.configure(objectMapper); - } - } - } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java index 118f48affb..3709b4afb8 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java @@ -91,17 +91,6 @@ public class RepositoryRestMvcAutoConfigurationTests { bean.getBaseUri()); } - @Test - public void propertiesStillAppliedWithCustomBootConfig() { - load(TestConfigurationWithRestMvcBootConfig.class, - "spring.data.rest.base-path:foo"); - assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class)); - RepositoryRestConfiguration bean = this.context - .getBean(RepositoryRestConfiguration.class); - assertEquals("Custom base URI should have been set", URI.create("/foo"), - bean.getBaseUri()); - } - @Test public void objectMappersAreConfiguredUsingObjectMapperBuilder() throws JsonProcessingException { @@ -144,11 +133,6 @@ public class RepositoryRestMvcAutoConfigurationTests { } - @Import({ TestConfiguration.class, SpringBootRepositoryRestMvcConfiguration.class }) - protected static class TestConfigurationWithRestMvcBootConfig { - - } - @Configuration @TestAutoConfigurationPackage(City.class) @EnableWebMvc diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index 72edfb9a60..f7bbe81e3a 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -1476,15 +1476,16 @@ respectively. [[howto-use-exposing-spring-data-repositories-rest-endpoint]] === Expose Spring Data repositories as REST endpoint + Spring Data REST can expose the `Repository` implementations as REST endpoints for you as long as Spring MVC has been enabled for the application. Spring Boot exposes as set of useful properties from the `spring.data.rest` namespace that -customize the {spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[`RepositoryRestConfiguration`]. -If you need to provide additional customization, you can create a `@Configuration` class -that extends `SpringBootRepositoryRestMvcConfiguration`. This class supports the same -functionality as `RepositoryRestMvcConfiguration`, but allows you to continue using -`spring.data.rest.*` properties. +customize the +{spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[`RepositoryRestConfiguration`]. +If you need to provide additional customization, you should use a +{spring-data-rest-javadoc}/core/config/RepositoryRestConfigurer.{dc-ext}[`RepositoryRestConfigurer`] +bean.