I have a use case where I have a list of hosts, I need to loop over those hosts and make the same api call for each of them. I don't want a hardcoded base url as that would mean creating a new feign client for each api call.
I would like to do something like:
public class FeignBaseUrlParamTest {
@Test
public void testIt() {
MyClient client = Feign.builder().target(Target.EmptyTarget.create(MyClient.class));
client.internalService("http://localhost:8080");
}
}
interface MyClient {
@RequestLine("GET {baseUrl}/internal-service")
String internalService(@Param("baseUrl") String host);
}
However this encodes the base url part and the following exception occurs:
feign.RetryableException: no protocol: http%3A//localhost%3A8080/internal-service executing GET http%3A//localhost%3A8080/internal-service
I tried to create a Target implementation which gets the url out and decodes everything before the path. However it is not possible as you can only perform additions to the url from that context.
I think you want to just add an unannotated URI param
interface MyClient {
@RequestLine("GET /internal-service")
String internalService(URI baseUrl);
}
That works perfectly. Thanks
@adriancole your suggestion works but I couldn't find any reference to this in the docs (or anywhere else). Can you please link to the source of this?
adriancole your suggestion works but I couldn't find any reference to this in the docs (or anywhere else). Can you please link to the source of this?
Adrian is no longer actively working on feign... anyways, there is a good chance the docs are incomplete, would love any contributions on that front.
@velo I'll be glad to contribute but I'm not familiar enough with the code to know where to start. Can you please point to the general direction in the code where this feature is handled?
Ow, sorry, I tough it was just documentation, my bad.
I would say to start with a unit test with
interface MyClient { @RequestLine("GET /internal-service") String internalService(URI baseUrl); }
And see where it breaks.
From there I can assist you.
It should be supported. There is a unit test already. It is missing from the docs however.
ha, thanks Kevin
Then all needed is to updated the README.md
Most helpful comment
I think you want to just add an unannotated URI param
interface MyClient {
@RequestLine("GET /internal-service")
String internalService(URI baseUrl);
}