From 3e05329fd733456fd1fb60629b26bf0c64baeff2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 16 Jan 2017 10:41:44 +0100 Subject: [PATCH] Order internal RepositoryRestConfigurer 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 gh-7981 --- .../SpringBootRepositoryRestConfigurer.java | 4 ++- ...positoryRestMvcAutoConfigurationTests.java | 35 ++++++++++++++++++- spring-boot-docs/src/main/asciidoc/howto.adoc | 4 +++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java index 2b602ebf79e..22791583381 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java @@ -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. @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data.rest; 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; @@ -32,6 +33,7 @@ import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; * @author Andy Wilkinson * @author Stephane Nicoll */ +@Order(0) class SpringBootRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter { @Autowired(required = false) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java index 0ccfbfe869c..f238b73b6f3 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java @@ -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. @@ -40,6 +40,7 @@ import org.springframework.context.annotation.Import; 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; @@ -54,6 +55,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Rob Winch * @author Andy Wilkinson + * @author Stephane Nicoll */ public class RepositoryRestMvcAutoConfigurationTests { @@ -118,6 +120,22 @@ public class RepositoryRestMvcAutoConfigurationTests { 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"); @@ -179,6 +197,11 @@ public class RepositoryRestMvcAutoConfigurationTests { } + @Import({ TestConfiguration.class, TestRepositoryRestConfigurer.class }) + protected static class TestConfigurationWithConfigurer { + + } + @Import({ TestConfiguration.class, RepositoryRestMvcConfiguration.class }) protected static class TestConfigurationWithRestMvcConfig { @@ -198,4 +221,14 @@ public class RepositoryRestMvcAutoConfigurationTests { } + 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); + } + } + } diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index 46ab50b51ca..d3b0842315f 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -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]]