Issue
Using spring boot 2.1.3.RELEASE, WebClient
will stop using the provided baseUrl
when passing an URI
to uri()
method. It will keep baseUrl
when a string is passed to uri()
though.
How can I provide a baseUrl
and pass an URI
?
public WebClient webClient() {
return WebClient.builder()
.baseUrl("https://example.com/")
.build();
}
and
webClient.get().uri(URI.create("/foo/%23bar"))...
throws
java.lang.IllegalArgumentException: URI is not absolute:
and the request url becomes
request url: /foo/%23bar
Solution
If you pass new URI Object, you override base URI.
You should use uri
method with lambda as a parameter, like in example:
final WebClient webClient = WebClient
.builder()
.baseUrl("http://localhost")
.build();
webClient
.get()
.uri(uriBuilder -> uriBuilder.pathSegment("api", "v2", "json", "test").build())
.exchange();
Answered By - Konstantin Pozhidaev
Answer Checked By - Candace Johnson (JavaFixing Volunteer)