WebClient URI template variables from bodyValue?

1.5k views Asked by At

I recently encountered some unexpected behaviour and was wondering if this was in fact intended functionality when using WebClient. With a client config as shown below, the uri variables in the template are being overridden with the fields from a POJO used as an arg to bodyValue.

class ExampleRequest {
   private String namespace = "jar";
   private String service = "zoo";
  ...
}
...
this.client = WebClient.builder()
                .baseUrl("http://{service}.{namespace}.svc.cluster.local")
                .defaultUriVariables(Map.of("service", "foo", "namespace", "bar"))
                .build();

this.client.post()
  .uri("/baz")
  .bodyValue(new ExampleRequest())
  .retrieve()
  ...

The above ends up calling out to http://zoo.jar.svc.cluster.local/baz, not http://foo.bar.svc.cluster.local/baz. I wasn't expecting the body of the message in this instance to be picked up as uri variables. :confused:

This seems strange to me, but if this is expected could point me to the source/docs of where this expansion is taking place..?

1

There are 1 answers

0
Vasif On

Could you please use standard HashMap implementation and recheck? I tested locally for me it is working as expected, I mean foo and bar chosen:

    Map<String, String > params = new HashMap<>();
    params.put("service", "foo");
    params.put("namespace", "bar");
    WebClient client = WebClient.builder()
            .baseUrl("http://{service}.{namespace}.svc.cluster.local")
            .defaultUriVariables(params)
            .build();

enter image description here