Skip to content

Commit 85d4ad7

Browse files
committed
Merge remote-tracking branch 'origin/4.0.x'
2 parents 90d6344 + 5ccec84 commit 85d4ad7

File tree

7 files changed

+117
-36
lines changed

7 files changed

+117
-36
lines changed

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
* @author Hyeonmin Park
7373
* @author Felix Dittrich
7474
* @author Dominique Villard
75+
* @athor Can Bezmen
7576
*/
7677
public class FeignClientFactoryBean
7778
implements FactoryBean<Object>, InitializingBean, ApplicationContextAware, BeanFactoryAware {
@@ -448,7 +449,6 @@ <T> T getTarget() {
448449
if (StringUtils.hasText(url) && !url.startsWith("http")) {
449450
url = "http://" + url;
450451
}
451-
String url = this.url + cleanPath();
452452
Client client = getOptional(feignClientFactory, Client.class);
453453
if (client != null) {
454454
if (client instanceof FeignBlockingLoadBalancerClient) {
@@ -489,14 +489,14 @@ private String cleanPath() {
489489
@SuppressWarnings({ "unchecked", "rawtypes" })
490490
private <T> HardCodedTarget<T> resolveTarget(FeignClientFactory context, String contextId, String url) {
491491
if (StringUtils.hasText(url)) {
492-
return new HardCodedTarget(type, name, url);
492+
return new HardCodedTarget(type, name, url + cleanPath());
493493
}
494494

495495
if (refreshableClient) {
496496
RefreshableUrl refreshableUrl = context.getInstance(contextId,
497497
RefreshableUrl.class.getCanonicalName() + "-" + contextId, RefreshableUrl.class);
498498
if (Objects.nonNull(refreshableUrl) && StringUtils.hasText(refreshableUrl.getUrl())) {
499-
return new RefreshableHardCodedTarget<>(type, name, refreshableUrl);
499+
return new RefreshableHardCodedTarget<>(type, name, refreshableUrl, cleanPath());
500500
}
501501
}
502502
FeignClientProperties.FeignClientConfiguration config = findConfigByKey(contextId);
@@ -505,7 +505,7 @@ private <T> HardCodedTarget<T> resolveTarget(FeignClientFactory context, String
505505
"Provide Feign client URL either in @FeignClient() or in config properties.");
506506
}
507507

508-
return new PropertyBasedTarget(type, name, config);
508+
return new PropertyBasedTarget(type, name, config, cleanPath());
509509
}
510510

511511
private boolean isUrlAvailableInConfig(String contextId) {

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* `spring.cloud.openfeign.client.config.[clientId].url`.
2626
*
2727
* @author Olga Maciaszek-Sharma
28+
* @author Can Bezmen
2829
* @see FeignClientProperties.FeignClientConfiguration#getUrl()
2930
*/
3031
public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> {
@@ -33,15 +34,25 @@ public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> {
3334

3435
private final FeignClientProperties.FeignClientConfiguration config;
3536

37+
private final String path;
38+
39+
public PropertyBasedTarget(Class<T> type, String name, FeignClientProperties.FeignClientConfiguration config,
40+
String path) {
41+
super(type, name, config.getUrl());
42+
this.config = config;
43+
this.path = path;
44+
}
45+
3646
public PropertyBasedTarget(Class<T> type, String name, FeignClientProperties.FeignClientConfiguration config) {
3747
super(type, name, config.getUrl());
3848
this.config = config;
49+
path = "";
3950
}
4051

4152
@Override
4253
public String url() {
4354
if (url == null) {
44-
url = config.getUrl();
55+
url = config.getUrl() + path;
4556
}
4657
return url;
4758
}

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/RefreshableHardCodedTarget.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,21 +22,32 @@
2222
* This target provides url wrapped under {@link Target}.
2323
*
2424
* @author Jasbir Singh
25+
* @author Olga Maciaszek-Sharma
2526
* @since 4.0.0
2627
*/
2728
public class RefreshableHardCodedTarget<T> extends Target.HardCodedTarget<T> {
2829

2930
private final RefreshableUrl refreshableUrl;
3031

32+
private final String cleanPath;
33+
3134
@SuppressWarnings("unchecked")
3235
public RefreshableHardCodedTarget(Class type, String name, RefreshableUrl refreshableUrl) {
3336
super(type, name, refreshableUrl.getUrl());
3437
this.refreshableUrl = refreshableUrl;
38+
cleanPath = "";
39+
}
40+
41+
@SuppressWarnings("unchecked")
42+
public RefreshableHardCodedTarget(Class type, String name, RefreshableUrl refreshableUrl, String cleanPath) {
43+
super(type, name, refreshableUrl.getUrl());
44+
this.refreshableUrl = refreshableUrl;
45+
this.cleanPath = cleanPath;
3546
}
3647

3748
@Override
3849
public String url() {
39-
return refreshableUrl.getUrl();
50+
return refreshableUrl.getUrl() + cleanPath;
4051
}
4152

4253
}

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
/**
3535
* @author Jasbir Singh
3636
* @author Olga Maciaszek-Sharma
37+
* @author Alex Elin
3738
*/
3839
@SpringBootTest
3940
@TestPropertySource("classpath:feign-properties.properties")
@@ -56,12 +57,6 @@ void shouldInstantiateFeignClientWhenUrlFromFeignClientUrl() {
5657
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
5758
}
5859

59-
@Test
60-
void shouldInstantiateFeignClientWhenUrlFromFeignClientUrlGivenPreferenceOverProperties() {
61-
UrlTestClient.UrlResponseForTests response = feignClientWithFixUrl.fixPath();
62-
assertThat(response.getUrl()).isEqualTo("http://localhost:8081/fixPath");
63-
}
64-
6560
@Test
6661
public void shouldInstantiateFeignClientWhenUrlFromProperties() {
6762
UrlTestClient.UrlResponseForTests response = configBasedClient.test();
@@ -76,11 +71,28 @@ void shouldInstantiateFeignClientWhenUrlFromFeignClientName() {
7671
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
7772
}
7873

74+
@Test
75+
void shouldInstantiateFeignClientWhenUrlAndPathAreInTheFeignClientAnnotation(
76+
@Autowired Application.WithPathAndFixedUrlClient client) {
77+
UrlTestClient.UrlResponseForTests response = client.test();
78+
assertThat(response.getUrl()).isEqualTo("http://localhost:7777/common/test");
79+
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
80+
}
81+
82+
@Test
83+
void shouldInstantiateFeignClientWhenUrlFromPropertiesAndPathInTheFeignClientAnnotation(
84+
@Autowired Application.WithPathAndUrlFromConfigClient client) {
85+
UrlTestClient.UrlResponseForTests response = client.test();
86+
assertThat(response.getUrl()).isEqualTo("http://localhost:7777/common/test");
87+
assertThat(response.getTargetType()).isEqualTo(PropertyBasedTarget.class);
88+
}
89+
7990
@Configuration
8091
@EnableAutoConfiguration
8192
@EnableConfigurationProperties(FeignClientProperties.class)
8293
@EnableFeignClients(clients = { Application.FeignClientWithFixUrl.class, Application.ConfigBasedClient.class,
83-
Application.NameBasedUrlClient.class })
94+
Application.NameBasedUrlClient.class, Application.WithPathAndUrlFromConfigClient.class,
95+
Application.WithPathAndFixedUrlClient.class })
8496
protected static class Application {
8597

8698
@Bean
@@ -112,6 +124,22 @@ protected interface NameBasedUrlClient {
112124

113125
}
114126

127+
@FeignClient(name = "withPathAndFixUrlClient", path = "/common", url = "http://localhost:7777")
128+
protected interface WithPathAndFixedUrlClient {
129+
130+
@GetMapping("/test")
131+
UrlTestClient.UrlResponseForTests test();
132+
133+
}
134+
135+
@FeignClient(name = "withPathAndUrlFromConfigClient", path = "/common")
136+
protected interface WithPathAndUrlFromConfigClient {
137+
138+
@GetMapping("/test")
139+
UrlTestClient.UrlResponseForTests test();
140+
141+
}
142+
115143
}
116144

117145
}

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/RefreshableFeignClientUrlTests.java

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@
2424
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.cloud.context.scope.refresh.RefreshScope;
27-
import org.springframework.context.ApplicationContext;
2827
import org.springframework.context.annotation.Bean;
2928
import org.springframework.context.annotation.Configuration;
3029
import org.springframework.test.annotation.DirtiesContext;
@@ -35,26 +34,24 @@
3534

3635
/**
3736
* @author Jasbir Singh
37+
* @author Olga Maciaszek-Sharma
3838
*/
3939
@SpringBootTest
4040
@TestPropertySource("classpath:feign-refreshable-properties.properties")
4141
@DirtiesContext
4242
class RefreshableFeignClientUrlTests {
4343

44-
@Autowired
45-
private ApplicationContext applicationContext;
46-
4744
@Autowired
4845
private RefreshScope refreshScope;
4946

5047
@Autowired
51-
private RefreshableFeignClientUrlTests.Application.RefreshableClientWithFixUrl refreshableClientWithFixUrl;
48+
private RefreshableFeignClientUrlTests.Application.RefreshableClientWithFixedUrl refreshableClientWithFixedUrl;
5249

5350
@Autowired
5451
private RefreshableFeignClientUrlTests.Application.RefreshableUrlClient refreshableUrlClient;
5552

5653
@Autowired
57-
private Application.RefreshableUrlClientForContextRefreshCase refreshableUrlClientForContextRefreshCase;
54+
private Application.RefreshableUrlClientForContextRefreshCase refreshableClientForContextRefreshCase;
5855

5956
@Autowired
6057
private Application.NameBasedUrlClient nameBasedUrlClient;
@@ -63,16 +60,18 @@ class RefreshableFeignClientUrlTests {
6360
private FeignClientProperties clientProperties;
6461

6562
@Test
66-
void shouldInstantiateFeignClientWhenUrlFromFeignClientUrl() {
67-
UrlTestClient.UrlResponseForTests response = refreshableClientWithFixUrl.fixPath();
68-
assertThat(response.getUrl()).isEqualTo("http://localhost:8081/fixPath");
63+
void shouldInstantiateFeignClientWhenUrlFromAnnotation() {
64+
UrlTestClient.UrlResponseForTests response = refreshableClientWithFixedUrl.fixedPath();
65+
assertThat(response.getUrl()).isEqualTo("http://localhost:8081/fixedPath");
6966
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
7067
}
7168

7269
@Test
73-
void shouldInstantiateFeignClientWhenUrlFromFeignClientUrlGivenPreferenceOverProperties() {
74-
UrlTestClient.UrlResponseForTests response = refreshableClientWithFixUrl.fixPath();
75-
assertThat(response.getUrl()).isEqualTo("http://localhost:8081/fixPath");
70+
void shouldInstantiateFeignClientWhenUrlAndPathFromAnnotation(
71+
@Autowired Application.WithPathAndFixedUrlClient client) {
72+
UrlTestClient.UrlResponseForTests response = client.test();
73+
assertThat(response.getUrl()).isEqualTo("http://localhost:7777/common/test");
74+
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
7675
}
7776

7877
@Test
@@ -84,15 +83,28 @@ public void shouldInstantiateFeignClientWhenUrlFromProperties() {
8483

8584
@Test
8685
void shouldInstantiateFeignClientWhenUrlFromPropertiesAndThenUpdateUrlWhenContextRefresh() {
87-
UrlTestClient.UrlResponseForTests response = refreshableUrlClientForContextRefreshCase.refreshable();
86+
UrlTestClient.UrlResponseForTests response = refreshableClientForContextRefreshCase.refreshable();
8887
assertThat(response.getUrl()).isEqualTo("http://localhost:8080/refreshable");
8988

90-
clientProperties.getConfig().get("refreshableClient").setUrl("http://localhost:8888/");
89+
clientProperties.getConfig().get("refreshableClientForContextRefreshCase").setUrl("http://localhost:8888");
9190
refreshScope.refreshAll();
92-
response = refreshableUrlClient.refreshable();
91+
response = refreshableClientForContextRefreshCase.refreshable();
9392
assertThat(response.getUrl()).isEqualTo("http://localhost:8888/refreshable");
9493
}
9594

95+
@Test
96+
void shouldInstantiateFeignClientWhenUrlFromPropertiesAndPathFromAnnotationThenUpdateUrlWhenContextRefresh(
97+
@Autowired Application.RefreshableUrlClientForContextRefreshCaseWithPath client) {
98+
UrlTestClient.UrlResponseForTests response = client.refreshable();
99+
assertThat(response.getUrl()).isEqualTo("http://localhost:8080/common/refreshable");
100+
101+
clientProperties.getConfig().get("refreshableClientForContextRefreshCaseWithPath")
102+
.setUrl("http://localhost:8888");
103+
refreshScope.refreshAll();
104+
response = client.refreshable();
105+
assertThat(response.getUrl()).isEqualTo("http://localhost:8888/common/refreshable");
106+
}
107+
96108
@Test
97109
void shouldInstantiateFeignClientWhenUrlFromFeignClientName() {
98110
UrlTestClient.UrlResponseForTests response = nameBasedUrlClient.nonRefreshable();
@@ -104,20 +116,21 @@ void shouldInstantiateFeignClientWhenUrlFromFeignClientName() {
104116
@EnableAutoConfiguration
105117
@EnableConfigurationProperties(FeignClientProperties.class)
106118
@EnableFeignClients(clients = { Application.RefreshableUrlClient.class, Application.NameBasedUrlClient.class,
107-
Application.RefreshableClientWithFixUrl.class,
108-
Application.RefreshableUrlClientForContextRefreshCase.class })
119+
Application.RefreshableClientWithFixedUrl.class,
120+
Application.RefreshableUrlClientForContextRefreshCase.class, Application.WithPathAndFixedUrlClient.class,
121+
Application.RefreshableUrlClientForContextRefreshCaseWithPath.class })
109122
protected static class Application {
110123

111124
@Bean
112125
UrlTestClient client() {
113126
return new UrlTestClient();
114127
}
115128

116-
@FeignClient(name = "refreshableClientWithFixUrl", url = "http://localhost:8081")
117-
protected interface RefreshableClientWithFixUrl {
129+
@FeignClient(name = "refreshableClientWithFixedUrl", url = "http://localhost:8081")
130+
protected interface RefreshableClientWithFixedUrl {
118131

119-
@GetMapping("/fixPath")
120-
UrlTestClient.UrlResponseForTests fixPath();
132+
@GetMapping("/fixedPath")
133+
UrlTestClient.UrlResponseForTests fixedPath();
121134

122135
}
123136

@@ -137,6 +150,14 @@ protected interface RefreshableUrlClientForContextRefreshCase {
137150

138151
}
139152

153+
@FeignClient(name = "refreshableClientForContextRefreshCaseWithPath", path = "/common")
154+
protected interface RefreshableUrlClientForContextRefreshCaseWithPath {
155+
156+
@GetMapping("/refreshable")
157+
UrlTestClient.UrlResponseForTests refreshable();
158+
159+
}
160+
140161
@FeignClient(name = "nameBasedClient")
141162
protected interface NameBasedUrlClient {
142163

@@ -145,6 +166,14 @@ protected interface NameBasedUrlClient {
145166

146167
}
147168

169+
@FeignClient(name = "withPathAndFixUrlClient", path = "/common", url = "http://localhost:7777")
170+
protected interface WithPathAndFixedUrlClient {
171+
172+
@GetMapping("/test")
173+
UrlTestClient.UrlResponseForTests test();
174+
175+
}
176+
148177
}
149178

150179
}

spring-cloud-openfeign-core/src/test/resources/feign-properties.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ spring.cloud.openfeign.client.config.connectTimeout.connectTimeout=1000
2828
spring.cloud.openfeign.client.config.default.followRedirects=false
2929
spring.cloud.openfeign.client.config.feignClientWithFixUrl.url=http://localhost:8888
3030
spring.cloud.openfeign.client.config.configBasedClient.url=http://localhost:9999
31+
spring.cloud.openfeign.client.config.withPathAndUrlFromConfigClient.url=http://localhost:7777

spring-cloud-openfeign-core/src/test/resources/feign-refreshable-properties.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ spring.cloud.openfeign.client.config.readTimeout.readTimeout=2000
1111
spring.cloud.openfeign.client.config.refreshableClient.url=http://localhost:8082
1212
spring.cloud.openfeign.client.config.refreshableClientWithFixUrl.url=http://localhost:8888
1313
spring.cloud.openfeign.client.config.refreshableClientForContextRefreshCase.url=http://localhost:8080
14+
spring.cloud.openfeign.client.config.refreshableClientForContextRefreshCaseWithPath.url=http://localhost:8080

0 commit comments

Comments
 (0)