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.

Comment From: codefromthecrypt

I think you want to just add an unannotated URI param

interface MyClient { @RequestLine("GET /internal-service") String internalService(URI baseUrl); }

Comment From: tomradcliffe

That works perfectly. Thanks

Comment From: asherbar

@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?

Comment From: velo

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.

Comment From: asherbar

@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?

Comment From: velo

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.

Comment From: kdavisk6

It should be supported. There is a unit test already. It is missing from the docs however.

Comment From: velo

ha, thanks Kevin

Then all needed is to updated the README.md

Comment From: batmanjo

Cool works ! but i didn't see it in https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#spring-cloud-feign. maybe it should be added to the official documentation。

Comment From: asgh

You can find the source for it here: https://github.com/OpenFeign/feign/blob/0cfff9c7a6f4607a28188f58d0b36c36a83fb327/core/src/main/java/feign/Contract.java#L141 and https://github.com/OpenFeign/feign/blob/0cfff9c7a6f4607a28188f58d0b36c36a83fb327/core/src/main/java/feign/RequestTemplateFactoryResolver.java#L91