Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of setting dynamic URI through method parameters #627

Open
mkrasilnikow opened this issue Sep 28, 2023 · 2 comments
Open

Add support of setting dynamic URI through method parameters #627

mkrasilnikow opened this issue Sep 28, 2023 · 2 comments

Comments

@mkrasilnikow
Copy link

In Feign client I can change URI in runtime, without creating new instance of Feign client through builder, like:

    @FeignClient(name = "dummy-name", url = "this-is-a-placeholder")
    public interface MyClient {
        @PostMapping(path = "/create")
        UserDto createUser(URI baseUrl, @RequestBody UserDto userDto);
    }
   //call service:
    URI determinedBasePathUri = URI.create("https://my-host.com");
    myClient.createUser(determinedBasePathUri, userDto);

But in ReactiveFeign I can't do so. Only through WebReactiveFeign .builder like this:

IcecreamServiceApi client = 
         WebReactiveFeign  //WebClient based reactive feign  
        .<IcecreamServiceApi>builder()
        .target(IcecreamServiceApi.class, "http://www.myUrl.com")

Is it possible to reuse the approach from Feign to ReactiveFeign client?

Thanks!

@mkrasilnikow
Copy link
Author

@marcusvoltolim
Copy link

Workaround with Java21 using ReactiveHttpRequestInterceptor and @RequestHeader:

@ReactiveFeignClient(name = "ReactiveDynamicUrlApi", url = BASE_URL, path = "v1/api/product", configuration = ReactiveDynamicUrlApi.RequestInterceptor.class)
interface ReactiveDynamicUrlApi {

    String BASE_URL = "base-url";

    @PostMapping(path = "{id}", headers = "Content-Type=application/json")
    Mono<String> post(@RequestHeader(BASE_URL) String baseUrl, @PathVariable String id, @RequestHeader String token, @RequestBody Map requestBody);


    class RequestInterceptor implements ReactiveHttpRequestInterceptor {

        @Override
        public Mono<ReactiveHttpRequest> apply(final ReactiveHttpRequest request) {
            var uri = URI.create(request.headers().get(BASE_URL).getFirst() + request.uri().getPath());
            request.headers().remove(BASE_URL);
            return Mono.just(new ReactiveHttpRequest(request, uri));
        }

    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants