diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java index 27df9616..fae7ec01 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java @@ -72,6 +72,7 @@ import org.springframework.util.StringUtils; * @author Hyeonmin Park * @author Felix Dittrich * @author Dominique Villard + * @athor Can Bezmen */ public class FeignClientFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware, BeanFactoryAware { @@ -495,7 +496,7 @@ public class FeignClientFactoryBean RefreshableUrl refreshableUrl = context.getInstance(contextId, RefreshableUrl.class.getCanonicalName() + "-" + contextId, RefreshableUrl.class); if (Objects.nonNull(refreshableUrl) && StringUtils.hasText(refreshableUrl.getUrl())) { - return new RefreshableHardCodedTarget<>(type, name, refreshableUrl); + return new RefreshableHardCodedTarget<>(type, name, refreshableUrl, cleanPath()); } } FeignClientProperties.FeignClientConfiguration config = findConfigByKey(contextId); diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java index f7dfb291..fdc471d2 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java @@ -25,6 +25,7 @@ import feign.Target; * `spring.cloud.openfeign.client.config.[clientId].url`. * * @author Olga Maciaszek-Sharma + * @author Can Bezmen * @see FeignClientProperties.FeignClientConfiguration#getUrl() */ public class PropertyBasedTarget extends Target.HardCodedTarget { @@ -42,6 +43,12 @@ public class PropertyBasedTarget extends Target.HardCodedTarget { this.path = path; } + public PropertyBasedTarget(Class type, String name, FeignClientProperties.FeignClientConfiguration config) { + super(type, name, config.getUrl()); + this.config = config; + path = ""; + } + @Override public String url() { if (url == null) { diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java index c811705b..f9788c96 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java @@ -57,12 +57,6 @@ class NonRefreshableFeignClientUrlTests { assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class); } - @Test - void shouldInstantiateFeignClientWhenUrlFromFeignClientUrlGivenPreferenceOverProperties() { - UrlTestClient.UrlResponseForTests response = feignClientWithFixUrl.fixPath(); - assertThat(response.getUrl()).isEqualTo("http://localhost:8081/fixPath"); - } - @Test public void shouldInstantiateFeignClientWhenUrlFromProperties() { UrlTestClient.UrlResponseForTests response = configBasedClient.test(); @@ -79,34 +73,26 @@ class NonRefreshableFeignClientUrlTests { @Test void shouldInstantiateFeignClientWhenUrlAndPathAreInTheFeignClientAnnotation( - @Autowired Application.WithPathAndFixUrlClient withPathAndFixUrlClient - ) { - UrlTestClient.UrlResponseForTests response = withPathAndFixUrlClient.test(); + @Autowired Application.WithPathAndFixedUrlClient client) { + UrlTestClient.UrlResponseForTests response = client.test(); assertThat(response.getUrl()).isEqualTo("http://localhost:7777/common/test"); assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class); } @Test void shouldInstantiateFeignClientWhenUrlFromPropertiesAndPathInTheFeignClientAnnotation( - @Autowired Application.WithPathAndUrlFromConfigClient withPathAndUrlFromConfigClient - ) { - UrlTestClient.UrlResponseForTests response = withPathAndUrlFromConfigClient.test(); + @Autowired Application.WithPathAndUrlFromConfigClient client) { + UrlTestClient.UrlResponseForTests response = client.test(); assertThat(response.getUrl()).isEqualTo("http://localhost:7777/common/test"); - assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class); + assertThat(response.getTargetType()).isEqualTo(PropertyBasedTarget.class); } @Configuration @EnableAutoConfiguration @EnableConfigurationProperties(FeignClientProperties.class) - @EnableFeignClients( - clients = { - Application.FeignClientWithFixUrl.class, - Application.ConfigBasedClient.class, - Application.NameBasedUrlClient.class, - Application.WithPathAndUrlFromConfigClient.class, - Application.WithPathAndFixUrlClient.class - } - ) + @EnableFeignClients(clients = { Application.FeignClientWithFixUrl.class, Application.ConfigBasedClient.class, + Application.NameBasedUrlClient.class, Application.WithPathAndUrlFromConfigClient.class, + Application.WithPathAndFixedUrlClient.class }) protected static class Application { @Bean @@ -139,7 +125,7 @@ class NonRefreshableFeignClientUrlTests { } @FeignClient(name = "withPathAndFixUrlClient", path = "/common", url = "http://localhost:7777") - protected interface WithPathAndFixUrlClient { + protected interface WithPathAndFixedUrlClient { @GetMapping("/test") UrlTestClient.UrlResponseForTests test();