From 48eb520e7e51a5d021c428066593a4799191311b Mon Sep 17 00:00:00 2001 From: spencergibb Date: Fri, 9 Oct 2020 21:13:39 -0400 Subject: [PATCH] Adds support for spring.cloud.config.media-type fixes gh-1715 --- .../config/client/ConfigClientProperties.java | 25 +++++++++++++++---- .../ConfigServicePropertySourceLocator.java | 5 ++-- ...nfigServicePropertySourceLocatorTests.java | 23 +++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) 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 3286de66..fbc10ac6 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; @@ -97,6 +98,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. */ @@ -208,6 +214,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; } @@ -365,11 +379,12 @@ public class ConfigClientProperties { 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 + ", token=" + this.token - + ", requestConnectTimeout=" + this.requestConnectTimeout - + ", requestReadTimeout=" + this.requestReadTimeout + ", sendState=" - + this.sendState + ", headers=" + this.headers + "]"; + + 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 29abbdfc..925b968b 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 @@ -258,8 +257,8 @@ 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 8b1da437..276463d3 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 @@ -90,6 +90,29 @@ public class ConfigServicePropertySourceLocatorTests { .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");