Skip to content

Commit

Permalink
Adopt RepositoryRestConfigurer and discourage subclassing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wilkinsona committed Jul 23, 2015
1 parent 9cd3b20 commit 24c63c9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -49,4 +50,9 @@
@Import(SpringBootRepositoryRestMvcConfiguration.class)
public class RepositoryRestMvcAutoConfiguration {

@Bean
public SpringBootRepositoryRestConfigurer springBootRepositoryRestConfigurer() {
return new SpringBootRepositoryRestConfigurer();
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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")
Expand All @@ -50,11 +40,4 @@ public RepositoryRestConfiguration config() {
return super.config();
}

@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
if (this.objectMapperBuilder != null) {
this.objectMapperBuilder.configure(objectMapper);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,6 @@ public void backOffWithCustomConfiguration() {
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 {
Expand Down Expand Up @@ -144,11 +133,6 @@ protected static class TestConfigurationWithRestMvcConfig {

}

@Import({ TestConfiguration.class, SpringBootRepositoryRestMvcConfiguration.class })
protected static class TestConfigurationWithRestMvcBootConfig {

}

@Configuration
@TestAutoConfigurationPackage(City.class)
@EnableWebMvc
Expand Down
11 changes: 6 additions & 5 deletions spring-boot-docs/src/main/asciidoc/howto.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.



Expand Down

0 comments on commit 24c63c9

Please sign in to comment.