From 4aaf8cd1881fbcb61d67f59a4cd1080a8a513358 Mon Sep 17 00:00:00 2001 From: Aizhan Date: Tue, 11 Aug 2015 14:04:30 -0600 Subject: [PATCH] avoid throwing htpp 404 error --- .../ConfigServicePropertySourceLocator.java | 16 +++++++++++++--- .../ConfigServicePropertySourceLocatorTests.java | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) 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 845347c9..8aeec429 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 @@ -39,6 +39,7 @@ import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.retry.annotation.Retryable; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; @@ -117,9 +118,18 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator args = new String[] { name, profile, label }; path = path + "/{label}"; } - ResponseEntity response = restTemplate.exchange(uri + path, - HttpMethod.GET, new HttpEntity((Void) null), - Environment.class, args); + ResponseEntity response = null; + + try { + response = restTemplate.exchange(uri + path, + HttpMethod.GET, new HttpEntity((Void) null), + Environment.class, args); + } catch (HttpClientErrorException e) { + if(e.getStatusCode() != HttpStatus.NOT_FOUND ) { + throw e; + } + } + if (response==null || response.getStatusCode()!=HttpStatus.OK) { return null; } 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 920186e9..4d1d8a32 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 @@ -7,6 +7,7 @@ import java.io.ByteArrayInputStream; import java.net.URI; import org.hamcrest.core.IsInstanceOf; +import org.hamcrest.core.IsNull; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -129,8 +130,7 @@ public class ConfigServicePropertySourceLocatorTests { Mockito.when(response.getBody()).thenReturn( new ByteArrayInputStream("".getBytes())); locator.setRestTemplate(restTemplate); - expected.expectCause(IsInstanceOf - . instanceOf(HttpClientErrorException.class)); + expected.expectCause(IsNull.nullValue(Throwable.class)); expected.expectMessage("fail fast property is set"); assertNull(locator.locate(environment)); }