Spring Web Reactive | 2. WebClient | 2.6. Attributes

You can add attributes to a request. This is useful when you want to pass information to the filter chain and influence how filters behave for a specific request. For example:

Java

WebClient client = WebClient.builder()
        .filter((request, next) -> {
            Optional<Object> usr = request.attribute("myAttribute");
            // ...
        })
        .build();

client.get().uri("https://example.org/")
        .attribute("myAttribute", "...")
        .retrieve()
        .bodyToMono(Void.class);

    }

Kotlin

val client = WebClient.builder()
        .filter { request, _ ->
            val usr = request.attributes()["myAttribute"];
            // ...
        }
        .build()

    client.get().uri("https://example.org/")
            .attribute("myAttribute", "...")
            .retrieve()
            .awaitBody<Unit>()

Note that you can use a defaultRequest callback at the WebClient.Builder level for global configuration. This lets you insert attributes into every request. For example, a Spring MVC application can use it to set request attributes based on ThreadLocal data.