Open mkrasilnikow opened 9 months ago
Original question from StackOverflow: https://stackoverflow.com/questions/65257778/using-dynamic-url-for-spring-reactivefeignclient
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));
}
}
}
In Feign client I can change URI in runtime, without creating new instance of Feign client through builder, like:
But in ReactiveFeign I can't do so. Only through WebReactiveFeign .builder like this:
Is it possible to reuse the approach from Feign to ReactiveFeign client?
Thanks!