Skip to content

Commit 1035e5b

Browse files
committed
Expose RepositoryRestMvcBootConfiguration
If an application defines a custom RepositoryRestMvcConfiguration, all Spring Boot defaults are lots. While this sounds sensible, it can be confusing as Spring Boot exposes properties (`spring.data.rest.*`) that are no longer honored. RepositoryRestMvcBootConfiguration is now public and can be used as an extension point for those who need to customize the Spring Data REST configuration and keep boot's specific defaults. Fixes gh-2392
1 parent 11b7fd8 commit 1035e5b

File tree

4 files changed

+91
-30
lines changed

4 files changed

+91
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,16 @@
1616

1717
package org.springframework.boot.autoconfigure.data.rest;
1818

19-
import org.springframework.beans.factory.annotation.Autowired;
2019
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2120
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2221
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2322
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2423
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2524
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
26-
import org.springframework.boot.context.properties.ConfigurationProperties;
27-
import org.springframework.context.annotation.Bean;
2825
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.context.annotation.Import;
2927
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
3028
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
31-
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
32-
33-
import com.fasterxml.jackson.databind.ObjectMapper;
3429

3530
/**
3631
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC
@@ -51,29 +46,7 @@
5146
@ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
5247
@ConditionalOnClass(RepositoryRestMvcConfiguration.class)
5348
@AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class)
49+
@Import(RepositoryRestMvcBootConfiguration.class)
5450
public class RepositoryRestMvcAutoConfiguration {
5551

56-
@Configuration
57-
static class RepositoryRestMvcBootConfiguration extends
58-
RepositoryRestMvcConfiguration {
59-
60-
@Autowired(required = false)
61-
private Jackson2ObjectMapperBuilder objectMapperBuilder;
62-
63-
@Bean
64-
@ConfigurationProperties(prefix = "spring.data.rest")
65-
@Override
66-
public RepositoryRestConfiguration config() {
67-
return super.config();
68-
}
69-
70-
@Override
71-
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
72-
if (this.objectMapperBuilder != null) {
73-
this.objectMapperBuilder.configure(objectMapper);
74-
}
75-
}
76-
77-
}
78-
7952
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.rest;
18+
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
26+
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
27+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
28+
29+
/**
30+
* A specialized {@link RepositoryRestMvcConfiguration} that applies configuration
31+
* items from the {@code spring.data.rest} namespace. Also configure Jackson if it's
32+
* available
33+
* <p>
34+
* Favor an extension of this class instead of extending directly from
35+
* {@link RepositoryRestMvcConfiguration}.
36+
*
37+
* @author Stephane Nicoll
38+
* @since 1.2.2
39+
*/
40+
@Configuration
41+
public class RepositoryRestMvcBootConfiguration extends
42+
RepositoryRestMvcConfiguration {
43+
44+
@Autowired(required = false)
45+
private Jackson2ObjectMapperBuilder objectMapperBuilder;
46+
47+
@Bean
48+
@ConfigurationProperties(prefix = "spring.data.rest")
49+
@Override
50+
public RepositoryRestConfiguration config() {
51+
return super.config();
52+
}
53+
54+
@Override
55+
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
56+
if (this.objectMapperBuilder != null) {
57+
this.objectMapperBuilder.configure(objectMapper);
58+
}
59+
}
60+
61+
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,16 @@ public void backOffWithCustomConfiguration() {
8989
.getBean(RepositoryRestConfiguration.class);
9090
assertEquals("Custom base URI should not have been set", URI.create(""),
9191
bean.getBaseUri());
92+
}
9293

94+
@Test
95+
public void propertiesStillAppliedWithCustomBootConfig() {
96+
load(TestConfigurationWithRestMvcBootConfig.class, "spring.data.rest.baseUri:foo");
97+
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
98+
RepositoryRestConfiguration bean = this.context
99+
.getBean(RepositoryRestConfiguration.class);
100+
assertEquals("Custom base URI should have been set", URI.create("foo"),
101+
bean.getBaseUri());
93102
}
94103

95104
@Test
@@ -134,6 +143,11 @@ protected static class TestConfigurationWithRestMvcConfig {
134143

135144
}
136145

146+
@Import({ TestConfiguration.class, RepositoryRestMvcBootConfiguration.class })
147+
protected static class TestConfigurationWithRestMvcBootConfig {
148+
149+
}
150+
137151
@Configuration
138152
@TestAutoConfigurationPackage(City.class)
139153
@EnableWebMvc

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,19 @@ repository types (Elasticsearch, Solr). Just change the names of the annotations
14641464
respectively.
14651465

14661466

1467+
[[howto-use-exposing-spring-data-repositories-rest-endpoint]]
1468+
=== Expose Spring Data repositories as REST endpoint
1469+
1470+
Spring Data REST can expose the `Repository` implementations as REST endpoints for you as
1471+
long as Spring MVC has been enabled for the application.
1472+
1473+
Spring Boot exposes as set of useful properties from the `spring.data.rest` namespace that
1474+
customize the
1475+
{spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[`RepositoryRestConfiguration`].
1476+
If your application requires to define its own `RepositoryRestMvcConfiguration` consider
1477+
extending from `RepositoryRestMvcBootConfiguration` instead as the latter provides namely
1478+
the handling of `spring.data.rest` properties.
1479+
14671480

14681481
[[howto-database-initialization]]
14691482
== Database initialization

0 commit comments

Comments
 (0)