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
pull/3588/head
Andy Wilkinson 9 years ago
parent 9cd3b20a78
commit 24c63c9b55

@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
@ -49,4 +50,9 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio
@Import(SpringBootRepositoryRestMvcConfiguration.class) @Import(SpringBootRepositoryRestMvcConfiguration.class)
public class RepositoryRestMvcAutoConfiguration { public class RepositoryRestMvcAutoConfiguration {
@Bean
public SpringBootRepositoryRestConfigurer springBootRepositoryRestConfigurer() {
return new SpringBootRepositoryRestConfigurer();
}
} }

@ -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);
}
}
}

@ -16,32 +16,22 @@
package org.springframework.boot.autoconfigure.data.rest; package org.springframework.boot.autoconfigure.data.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; 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 * 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.
* <p> * <p>
* Favor an extension of this class instead of extending directly from
* {@link RepositoryRestMvcConfiguration}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.2 * @since 1.2.2
*/ */
@Configuration @Configuration
public class SpringBootRepositoryRestMvcConfiguration extends class SpringBootRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {
RepositoryRestMvcConfiguration {
@Autowired(required = false)
private Jackson2ObjectMapperBuilder objectMapperBuilder;
@Bean @Bean
@ConfigurationProperties(prefix = "spring.data.rest") @ConfigurationProperties(prefix = "spring.data.rest")
@ -50,11 +40,4 @@ public class SpringBootRepositoryRestMvcConfiguration extends
return super.config(); return super.config();
} }
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
if (this.objectMapperBuilder != null) {
this.objectMapperBuilder.configure(objectMapper);
}
}
} }

@ -91,17 +91,6 @@ public class RepositoryRestMvcAutoConfigurationTests {
bean.getBaseUri()); 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 @Test
public void objectMappersAreConfiguredUsingObjectMapperBuilder() public void objectMappersAreConfiguredUsingObjectMapperBuilder()
throws JsonProcessingException { throws JsonProcessingException {
@ -144,11 +133,6 @@ public class RepositoryRestMvcAutoConfigurationTests {
} }
@Import({ TestConfiguration.class, SpringBootRepositoryRestMvcConfiguration.class })
protected static class TestConfigurationWithRestMvcBootConfig {
}
@Configuration @Configuration
@TestAutoConfigurationPackage(City.class) @TestAutoConfigurationPackage(City.class)
@EnableWebMvc @EnableWebMvc

@ -1476,15 +1476,16 @@ respectively.
[[howto-use-exposing-spring-data-repositories-rest-endpoint]] [[howto-use-exposing-spring-data-repositories-rest-endpoint]]
=== Expose Spring Data repositories as REST endpoint === Expose Spring Data repositories as REST endpoint
Spring Data REST can expose the `Repository` implementations as REST endpoints for you as Spring Data REST can expose the `Repository` implementations as REST endpoints for you as
long as Spring MVC has been enabled for the application. 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 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`]. customize the
If you need to provide additional customization, you can create a `@Configuration` class {spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[`RepositoryRestConfiguration`].
that extends `SpringBootRepositoryRestMvcConfiguration`. This class supports the same If you need to provide additional customization, you should use a
functionality as `RepositoryRestMvcConfiguration`, but allows you to continue using {spring-data-rest-javadoc}/core/config/RepositoryRestConfigurer.{dc-ext}[`RepositoryRestConfigurer`]
`spring.data.rest.*` properties. bean.

Loading…
Cancel
Save