Search code examples
javaspringconfigurationspring-webclient

How to retrieve the configured base URL from the Spring WebClient?


The Spring reactive WebClient can be built with a base URL:

import org.springframework.web.reactive.function.client.WebClient;
...
@Bean
public WebClient webClient(WebClient.Builder builder) {
    return builder
            .baseUrl("http://example.org")
            .build();
    // or alternatively a shortcut
    // return WebClient.create("http://example.org");
}

Is there a way to retrieve the configured base URL back from an already existing WebClient instance?

Something like:

@Autowired
private WebClient webClient;
...
String baseUrl = webClient.getBaseUrl(); // I want to know how this WebClient is configured
assertEquals("http://example.org", baseUrl);

Or something like

var configuration = webClient.getConfiguration(); 
String baseUrl = configuration.getBaseUrl();
assertEquals("http://example.org", baseUrl);

I understand that the handling of the parameter is internal and implementation specific. However I don't understand why, if it is the interface who exposes the setter (via the builder or the factory method argument), it also does not expose a getter. I am not specifying the implementation when creating the instance. So I would naturally expect the interface to tell me with what value it was created. I cannot see any plausible reason why this information is not exposed in the interface itself.


Solution

  • WebClient is an interface with no API to get back the base URL, so maybe some implementation class has one but that will make your code dependent on the chosen one.