Skip to content

Commit

Permalink
Order internal RepositoryRestConfigurer
Browse files Browse the repository at this point in the history
This commit provides an order of zero for the RepositoryRestConfigurer
that is used internally to configure the `RepositoryRestConfiguration`. In
practice, an unordered `RepositoryRestConfigurer` will run after ours.

Closes spring-projectsgh-7981
  • Loading branch information
snicoll committed Jan 16, 2017
1 parent ddf1408 commit 3e05329
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
Expand All @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
Expand All @@ -32,6 +33,7 @@
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
@Order(0)
class SpringBootRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter {

@Autowired(required = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
Expand Down Expand Up @@ -40,6 +40,7 @@
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
import org.springframework.data.rest.webmvc.BaseUri;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
Expand All @@ -54,6 +55,7 @@
*
* @author Rob Winch
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
public class RepositoryRestMvcAutoConfigurationTests {

Expand Down Expand Up @@ -118,6 +120,22 @@ public void testWithCustomSettings() throws Exception {
assertThat(bean.isEnableEnumTranslation()).isTrue();
}

@Test
public void testWithCustomConfigurer() {
load(TestConfigurationWithConfigurer.class,
"spring.data.rest.detection-strategy=visibility",
"spring.data.rest.default-media-type:application/my-json");
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class))
.isNotNull();
RepositoryRestConfiguration bean = this.context
.getBean(RepositoryRestConfiguration.class);
assertThat(bean.getRepositoryDetectionStrategy())
.isEqualTo(RepositoryDetectionStrategies.ALL);
assertThat(bean.getDefaultMediaType())
.isEqualTo(MediaType.parseMediaType("application/my-custom-json"));
assertThat(bean.getMaxPageSize()).isEqualTo(78);
}

@Test
public void backOffWithCustomConfiguration() {
load(TestConfigurationWithRestMvcConfig.class, "spring.data.rest.base-path:foo");
Expand Down Expand Up @@ -179,6 +197,11 @@ protected static class TestConfiguration {

}

@Import({ TestConfiguration.class, TestRepositoryRestConfigurer.class })
protected static class TestConfigurationWithConfigurer {

}

@Import({ TestConfiguration.class, RepositoryRestMvcConfiguration.class })
protected static class TestConfigurationWithRestMvcConfig {

Expand All @@ -198,4 +221,14 @@ public Jackson2ObjectMapperBuilder objectMapperBuilder() {

}

static class TestRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setRepositoryDetectionStrategy(RepositoryDetectionStrategies.ALL);
config.setDefaultMediaType(MediaType.parseMediaType(
"application/my-custom-json"));
config.setMaxPageSize(78);
}
}

}
4 changes: 4 additions & 0 deletions spring-boot-docs/src/main/asciidoc/howto.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,10 @@ If you need to provide additional customization, you should use a
{spring-data-rest-javadoc}/webmvc/config/RepositoryRestConfigurer.{dc-ext}[`RepositoryRestConfigurer`]
bean.

NOTE: If you don't specify any order on your custom `RepositoryRestConfigurer` it will run
after the one Spring Boot uses internally. If you need to specify an order, make sure it
is higher than 0.



[[howto-configure-a-component-that-is-used-by-JPA]]
Expand Down

0 comments on commit 3e05329

Please sign in to comment.