Commit 93d12494 authored by Stephane Nicoll's avatar Stephane Nicoll

Migrate Spring Data Rest settings

In order to have more control on the structure of the configuration, we
do not bind to `RepositoryRestConfiguration` directly anymore. This
commit introduces `RepositoryProperties` instead.

See gh-3854
Closes gh-4073
parent 2964fd28
......@@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
......@@ -50,6 +51,7 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio
@ConditionalOnClass(RepositoryRestMvcConfiguration.class)
@AutoConfigureAfter({ HttpMessageConvertersAutoConfiguration.class,
JacksonAutoConfiguration.class })
@EnableConfigurationProperties(RepositoryRestProperties.class)
@Import(SpringBootRepositoryRestMvcConfiguration.class)
public class RepositoryRestMvcAutoConfiguration {
......
/*
* 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.boot.context.properties.ConfigurationProperties;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.http.MediaType;
/**
* Configuration properties for Spring Data REST.
*
* @author Stephane Nicoll
* @since 1.3.0
*/
@ConfigurationProperties("spring.data.rest")
public class RepositoryRestProperties {
/**
* Base path to be used by Spring Data REST to expose repository resources.
*/
private String basePath;
/**
* Default size of pages.
*/
private Integer defaultPageSize;
/**
* Maximum size of pages.
*/
private Integer maxPageSize;
/**
* Name of the URL query string parameter that indicates what page to return.
*/
private String pageParamName;
/**
* Name of the URL query string parameter that indicates how many results to return at once.
*/
private String limitParamName;
/**
* Name of the URL query string parameter that indicates what direction to sort results.
*/
private String sortParamName;
/**
* Content type to use as a default when none is specified.
*/
private MediaType defaultMediaType;
/**
* Return a response body after creating an entity.
*/
private Boolean returnBodyOnCreate;
/**
* Return a response body after updating an entity.
*/
private Boolean returnBodyOnUpdate;
/**
* Enable enum value translation via the Spring Data REST default resource bundle. Will use
* the fully qualified enum name as key.
*/
private Boolean enableEnumTranslation;
public void applyTo(RepositoryRestConfiguration configuration) {
if (this.basePath != null) {
configuration.setBasePath(this.basePath);
}
if (this.defaultPageSize != null) {
configuration.setDefaultPageSize(this.defaultPageSize);
}
if (this.maxPageSize != null) {
configuration.setMaxPageSize(this.maxPageSize);
}
if (this.pageParamName != null) {
configuration.setPageParamName(this.pageParamName);
}
if (this.limitParamName != null) {
configuration.setLimitParamName(this.limitParamName);
}
if (this.sortParamName != null) {
configuration.setSortParamName(this.sortParamName);
}
if (this.defaultMediaType != null) {
configuration.setDefaultMediaType(this.defaultMediaType);
}
if (this.returnBodyOnCreate != null) {
configuration.setReturnBodyOnCreate(this.returnBodyOnCreate);
}
if (this.returnBodyOnUpdate != null) {
configuration.setReturnBodyOnUpdate(this.returnBodyOnUpdate);
}
if (this.enableEnumTranslation != null) {
configuration.setEnableEnumTranslation(this.enableEnumTranslation);
}
}
public String getBasePath() {
return this.basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
public Integer getDefaultPageSize() {
return this.defaultPageSize;
}
public void setDefaultPageSize(Integer defaultPageSize) {
this.defaultPageSize = defaultPageSize;
}
public Integer getMaxPageSize() {
return this.maxPageSize;
}
public void setMaxPageSize(Integer maxPageSize) {
this.maxPageSize = maxPageSize;
}
public String getPageParamName() {
return this.pageParamName;
}
public void setPageParamName(String pageParamName) {
this.pageParamName = pageParamName;
}
public String getLimitParamName() {
return this.limitParamName;
}
public void setLimitParamName(String limitParamName) {
this.limitParamName = limitParamName;
}
public String getSortParamName() {
return this.sortParamName;
}
public void setSortParamName(String sortParamName) {
this.sortParamName = sortParamName;
}
public MediaType getDefaultMediaType() {
return this.defaultMediaType;
}
public void setDefaultMediaType(MediaType defaultMediaType) {
this.defaultMediaType = defaultMediaType;
}
public Boolean getReturnBodyOnCreate() {
return this.returnBodyOnCreate;
}
public void setReturnBodyOnCreate(Boolean returnBodyOnCreate) {
this.returnBodyOnCreate = returnBodyOnCreate;
}
public Boolean getReturnBodyOnUpdate() {
return this.returnBodyOnUpdate;
}
public void setReturnBodyOnUpdate(Boolean returnBodyOnUpdate) {
this.returnBodyOnUpdate = returnBodyOnUpdate;
}
public Boolean getEnableEnumTranslation() {
return this.enableEnumTranslation;
}
public void setEnableEnumTranslation(Boolean enableEnumTranslation) {
this.enableEnumTranslation = enableEnumTranslation;
}
}
......@@ -16,7 +16,7 @@
package org.springframework.boot.autoconfigure.data.rest;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
......@@ -33,11 +33,15 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio
@Configuration
class SpringBootRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {
@Autowired
private RepositoryRestProperties properties;
@Bean
@ConfigurationProperties(prefix = "spring.data.rest")
@Override
public RepositoryRestConfiguration config() {
return super.config();
RepositoryRestConfiguration config = super.config();
this.properties.applyTo(config);
return config;
}
}
......@@ -37,6 +37,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.BaseUri;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
......@@ -87,6 +88,33 @@ public class RepositoryRestMvcAutoConfigurationTests {
baseUri.getUri());
}
@Test
public void testWithCustomSettings() throws Exception {
load(TestConfiguration.class,
"spring.data.rest.default-page-size:42",
"spring.data.rest.max-page-size:78",
"spring.data.rest.page-param-name:_page",
"spring.data.rest.limit-param-name:_limit",
"spring.data.rest.sort-param-name:_sort",
"spring.data.rest.default-media-type:application/my-json",
"spring.data.rest.return-body-on-create:false",
"spring.data.rest.return-body-on-update:false",
"spring.data.rest.enable-enum-translation:true");
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
RepositoryRestConfiguration bean = this.context
.getBean(RepositoryRestConfiguration.class);
assertEquals("Custom default page size not set", 42, bean.getDefaultPageSize());
assertEquals("Custom max page size not set", 78, bean.getMaxPageSize());
assertEquals("Custom page param name not set", "_page", bean.getPageParamName());
assertEquals("Custom limit param name not set", "_limit", bean.getLimitParamName());
assertEquals("Custom sort param name not set", "_sort", bean.getSortParamName());
assertEquals("Custom default media type not set",
MediaType.parseMediaType("application/my-json"), bean.getDefaultMediaType());
assertEquals("Custom return body on create flag not set", false, bean.returnBodyOnCreate(null));
assertEquals("Custom return body on update flag not set", false, bean.returnBodyOnUpdate(null));
assertEquals("Custom enable enum translation flag not set", true, bean.isEnableEnumTranslation());
}
@Test
public void backOffWithCustomConfiguration() {
load(TestConfigurationWithRestMvcConfig.class, "spring.data.rest.base-path:foo");
......
......@@ -472,8 +472,16 @@ content into your application; rather pick only the properties that you need.
spring.data.elasticsearch.properties.*= # Additional properties used to configure the client
spring.data.elasticsearch.repositories.enabled=true # if spring data repository support is enabled
# DATA REST ({spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[RepositoryRestConfiguration])
spring.data.rest.base-path= # base path against which the exporter should calculate its links
# DATA REST ({sc-spring-boot-autoconfigure}/data/rest/RepositoryRestProperties.{sc-ext}[RepositoryRestProperties])
spring.data.rest.base-path= # base path to be used by Spring Data REST to expose repository resources
spring.data.rest.default-page-size= # default size of pages
spring.data.rest.enable-enum-translation= # enable enum value translation via the Spring Data REST default resource bundle
spring.data.rest.limit-param-name= # name of the URL query string parameter that indicates how many results to return at once
spring.data.rest.max-page-size= # maximum size of pages
spring.data.rest.page-param-name= # name of the URL query string parameter that indicates what page to return
spring.data.rest.return-body-on-create= # return a response body after creating an entity
spring.data.rest.return-body-on-update= # return a response body after updating an entity
spring.data.rest.sort-param-name= # name of the URL query string parameter that indicates what direction to sort results
# FLYWAY ({sc-spring-boot-autoconfigure}/flyway/FlywayProperties.{sc-ext}[FlywayProperties])
flyway.*= # Any public property available on the auto-configured `Flyway` object
......
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