avoid throwing htpp 404 error

This commit is contained in:
Aizhan
2015-08-11 14:04:30 -06:00
committed by Spencer Gibb
parent 6bac89ae85
commit 4aaf8cd188
2 changed files with 15 additions and 5 deletions

View File

@@ -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<Environment> response = restTemplate.exchange(uri + path,
HttpMethod.GET, new HttpEntity<Void>((Void) null),
Environment.class, args);
ResponseEntity<Environment> response = null;
try {
response = restTemplate.exchange(uri + path,
HttpMethod.GET, new HttpEntity<Void>((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;
}

View File

@@ -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
.<Throwable> instanceOf(HttpClientErrorException.class));
expected.expectCause(IsNull.nullValue(Throwable.class));
expected.expectMessage("fail fast property is set");
assertNull(locator.locate(environment));
}