How to keep baseUrl when using uri() in spring webflux

A slightly different way - use path instead of pathSegment on existing uri object. It helps to maintain the path conveniently in configuration/constant form.

final WebClient webClient = WebClient
.builder()
.baseUrl("http://localhost")
.build();
webClient
.get()
.uri(uriBuilder -> uriBuilder.path("api/v2/json/test").build())
.exchange();

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();