Commit 1035e5b0 authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent 11b7fd83
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,21 +16,16 @@ ...@@ -16,21 +16,16 @@
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.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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.boot.context.properties.ConfigurationProperties;
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.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;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC * {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC
...@@ -51,29 +46,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -51,29 +46,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class) @ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
@ConditionalOnClass(RepositoryRestMvcConfiguration.class) @ConditionalOnClass(RepositoryRestMvcConfiguration.class)
@AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class) @AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class)
@Import(RepositoryRestMvcBootConfiguration.class)
public class RepositoryRestMvcAutoConfiguration { public class RepositoryRestMvcAutoConfiguration {
@Configuration
static class RepositoryRestMvcBootConfiguration extends
RepositoryRestMvcConfiguration {
@Autowired(required = false)
private Jackson2ObjectMapperBuilder objectMapperBuilder;
@Bean
@ConfigurationProperties(prefix = "spring.data.rest")
@Override
public RepositoryRestConfiguration config() {
return super.config();
}
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
if (this.objectMapperBuilder != null) {
this.objectMapperBuilder.configure(objectMapper);
}
}
}
} }
/*
* 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 com.fasterxml.jackson.databind.ObjectMapper;
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;
/**
* A specialized {@link RepositoryRestMvcConfiguration} that applies configuration
* items from the {@code spring.data.rest} namespace. Also configure Jackson if it's
* available
* <p>
* Favor an extension of this class instead of extending directly from
* {@link RepositoryRestMvcConfiguration}.
*
* @author Stephane Nicoll
* @since 1.2.2
*/
@Configuration
public class RepositoryRestMvcBootConfiguration extends
RepositoryRestMvcConfiguration {
@Autowired(required = false)
private Jackson2ObjectMapperBuilder objectMapperBuilder;
@Bean
@ConfigurationProperties(prefix = "spring.data.rest")
@Override
public RepositoryRestConfiguration config() {
return super.config();
}
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
if (this.objectMapperBuilder != null) {
this.objectMapperBuilder.configure(objectMapper);
}
}
}
...@@ -89,7 +89,16 @@ public class RepositoryRestMvcAutoConfigurationTests { ...@@ -89,7 +89,16 @@ public class RepositoryRestMvcAutoConfigurationTests {
.getBean(RepositoryRestConfiguration.class); .getBean(RepositoryRestConfiguration.class);
assertEquals("Custom base URI should not have been set", URI.create(""), assertEquals("Custom base URI should not have been set", URI.create(""),
bean.getBaseUri()); bean.getBaseUri());
}
@Test
public void propertiesStillAppliedWithCustomBootConfig() {
load(TestConfigurationWithRestMvcBootConfig.class, "spring.data.rest.baseUri: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
...@@ -134,6 +143,11 @@ public class RepositoryRestMvcAutoConfigurationTests { ...@@ -134,6 +143,11 @@ public class RepositoryRestMvcAutoConfigurationTests {
} }
@Import({ TestConfiguration.class, RepositoryRestMvcBootConfiguration.class })
protected static class TestConfigurationWithRestMvcBootConfig {
}
@Configuration @Configuration
@TestAutoConfigurationPackage(City.class) @TestAutoConfigurationPackage(City.class)
@EnableWebMvc @EnableWebMvc
......
...@@ -1464,6 +1464,19 @@ repository types (Elasticsearch, Solr). Just change the names of the annotations ...@@ -1464,6 +1464,19 @@ repository types (Elasticsearch, Solr). Just change the names of the annotations
respectively. 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 your application requires to define its own `RepositoryRestMvcConfiguration` consider
extending from `RepositoryRestMvcBootConfiguration` instead as the latter provides namely
the handling of `spring.data.rest` properties.
[[howto-database-initialization]] [[howto-database-initialization]]
== Database initialization == Database initialization
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment