title: Share data through a request lifecycle in Spring Webflux
body: <p>In Spring MVC I can use ThreadLocal to share data between different components through a request, and the data will be automatically cleared when the request is fulfilled. With WebFlux, since a request can be handled by multiple threads, this solution will not work. How to implement a similar solution so that initially a WebFilter can set some data in the request context, then the data can be accessed and modified in the controllers, and any event handlers that the request has gone through?</p>
  <p>I tried subscriberContext but it did not work. Here is my code. Why this does not work? Is there another way to share the data?</p>
  <pre><code>public Mono&lt;Void&gt; filter(ServerWebExchange exchange, WebFilterChain chain) {
      exchange.getResponse().beforeCommit(() -&gt; {
          return Mono.subscriberContext()
           .map(context -&gt; {
                context.hasKey(&quot;test&quot;); // this returns false
           })
      }
  
      return Mono.subscriberContext()
          .map(context -&gt; {context.put(&quot;test&quot;, &quot;test&quot;);})
          .flatMap(chain.filter(exchange))
  }
  </code></pre>