diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java index 901bbf3e..87191110 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java @@ -27,6 +27,7 @@ import javax.annotation.PostConstruct; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.config.environment.EnvironmentMediaType; import org.springframework.cloud.configuration.TlsProperties; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; @@ -102,6 +103,11 @@ public class ConfigClientProperties { */ private String[] uri = { "http://localhost:8888" }; + /** + * The Accept header media type to send to config server. + */ + private String mediaType = EnvironmentMediaType.V2_JSON; + /** * Discovery properties. */ @@ -213,6 +219,14 @@ public class ConfigClientProperties { return extractCredentials(index); } + public String getMediaType() { + return this.mediaType; + } + + public void setMediaType(String mediaType) { + this.mediaType = mediaType; + } + public Discovery getDiscovery() { return this.discovery; } @@ -364,7 +378,7 @@ public class ConfigClientProperties { public String toString() { return "ConfigClientProperties [enabled=" + this.enabled + ", profile=" + this.profile + ", name=" + this.name + ", label=" + this.label + ", username=" + this.username + ", password=" + this.password + ", uri=" - + Arrays.toString(this.uri) + ", discovery=" + this.discovery + ", failFast=" + this.failFast + + Arrays.toString(this.uri) + ", mediaType=" + this.mediaType + ", discovery=" + this.discovery + ", failFast=" + this.failFast + ", token=" + this.token + ", requestConnectTimeout=" + this.requestConnectTimeout + ", requestReadTimeout=" + this.requestReadTimeout + ", sendState=" + this.sendState + ", headers=" + this.headers + "]"; diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index cb42119b..37c643dd 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -71,7 +71,6 @@ import org.springframework.web.client.RestTemplate; import static org.springframework.cloud.config.client.ConfigClientProperties.AUTHORIZATION; import static org.springframework.cloud.config.client.ConfigClientProperties.STATE_HEADER; import static org.springframework.cloud.config.client.ConfigClientProperties.TOKEN_HEADER; -import static org.springframework.cloud.config.environment.EnvironmentMediaType.V2_JSON; /** * @author Dave Syer @@ -240,7 +239,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator try { HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Collections.singletonList(MediaType.parseMediaType(V2_JSON))); + headers.setAccept(Collections.singletonList(MediaType.parseMediaType(properties.getMediaType()))); addAuthorizationToken(properties, headers, username, password); if (StringUtils.hasText(token)) { headers.add(TOKEN_HEADER, token); diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java index e3848821..54ae88b4 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java @@ -89,6 +89,29 @@ public class ConfigServicePropertySourceLocatorTests { assertThat(httpEntity.getHeaders().getAccept()).containsExactly(MediaType.parseMediaType(V2_JSON)); } + @Test + public void customMediaType() { + Environment body = new Environment("app", "master"); + mockRequestResponseWithoutLabel(new ResponseEntity<>(body, HttpStatus.OK)); + ConfigClientProperties properties = new ConfigClientProperties(this.environment); + properties.setMediaType("application/json"); + ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator( + properties); + locator.setRestTemplate(this.restTemplate); + + ArgumentCaptor argumentCaptor = ArgumentCaptor + .forClass(HttpEntity.class); + + assertThat(locator.locateCollection(this.environment)).isNotNull(); + + Mockito.verify(this.restTemplate).exchange(anyString(), any(HttpMethod.class), + argumentCaptor.capture(), any(Class.class), anyString(), anyString()); + + HttpEntity httpEntity = argumentCaptor.getValue(); + assertThat(httpEntity.getHeaders().getAccept()) + .containsExactly(MediaType.parseMediaType("application/json")); + } + @Test public void sunnyDayWithLabel() { Environment body = new Environment("app", "master");