Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/spring-cloud-openfeign.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ TIP: If you want to run Spring Cloud OpenFeign clients in AOT or native image mo

TIP: If you want to run Spring Cloud OpenFeign clients in AOT or native image modes, ensure `spring.cloud.openfeign.lazy-attributes-resolution` has not been set to `true`.

TIP: However, if you set the `url` value via properties, it is possible to override the `@FeignClient` `url` value by running the image with `-Dspring.cloud.openfeign.client.config.[clientId].url=[url]` flag. In order to enable overriding, a `url` value also has to be set via properties and not `@FeignClient` attribute during buildtime.

== Configuration properties

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -505,7 +505,7 @@ private <T> HardCodedTarget<T> resolveTarget(FeignClientFactory context, String
"Provide Feign client URL either in @FeignClient() or in config properties.");
}

return new HardCodedTarget(type, name, FeignClientsRegistrar.getUrl(config.getUrl()));
return new PropertyBasedTarget(type, name, config);
}

private boolean isUrlAvailableInConfig(String contextId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.openfeign;

import feign.Target;

/**
* A {@link HardCodedTarget} implementation that resolves url from properties when the
* initial call is made. Using it allows specifying the url at runtime in an AOT-packaged
* application or a native image by setting the value of the
* `spring.cloud.openfeign.client.config.[clientId].url`.
*
* @author Olga Maciaszek-Sharma
* @see FeignClientProperties.FeignClientConfiguration#getUrl()
*/
public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> {

private String url;

private final FeignClientProperties.FeignClientConfiguration config;

public PropertyBasedTarget(Class<T> type, String name, FeignClientProperties.FeignClientConfiguration config) {
super(type, name, config.getUrl());
this.config = config;
}

@Override
public String url() {
if (url == null) {
url = config.getUrl();
}
return url;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@

/**
* @author Jasbir Singh
* @author Olga Maciaszek-Sharma
*/
@SpringBootTest
@TestPropertySource("classpath:feign-properties.properties")
Expand Down Expand Up @@ -65,7 +66,7 @@ void shouldInstantiateFeignClientWhenUrlFromFeignClientUrlGivenPreferenceOverPro
public void shouldInstantiateFeignClientWhenUrlFromProperties() {
UrlTestClient.UrlResponseForTests response = configBasedClient.test();
assertThat(response.getUrl()).isEqualTo("http://localhost:9999/test");
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
assertThat(response.getTargetType()).isEqualTo(PropertyBasedTarget.class);
}

@Test
Expand Down