Avatar
0
anaconda875 Beginner
anaconda875 Beginner
Spring weblux how to share data from Controller to Filter using Context
<div class="votecell post-layout--left"> <div class="js-voting-container d-flex jc-center fd-column ai-stretch gs4 fc-black-200"></div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body"> <p> </p> <p> In controller I need to share some data to WebFilter (I don't want to use <code>ServerWebExchange.getAttributes().put("KEY", "VALUE")</code>, and my boss either), this is my current implemetation: </p> <pre class="lang-java s-code-block">&lt;code&gt;&lt;span&gt;@Component&lt;/span&gt; &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;RequestFilter&lt;/span&gt; &lt;span&gt;implements&lt;/span&gt; &lt;span&gt;WebFilter&lt;/span&gt; { &lt;span&gt;@Override&lt;/span&gt; &lt;span&gt;public&lt;/span&gt; Mono&lt;Void&gt; &lt;span&gt;filter&lt;/span&gt;&lt;span&gt;(ServerWebExchange exchange, WebFilterChain chain)&lt;/span&gt; { exchange.getResponse().beforeCommit(() -&gt; Mono.deferWithContext(c -&gt; { &lt;span&gt;//I CHECK WHETHER DATA SHARED FROM CONTROLLER EXIST OR NOT&lt;/span&gt; System.out.println(c.hasKey(&lt;span&gt;"abc"&lt;/span&gt;)); &lt;span&gt;Context&lt;/span&gt; &lt;span&gt;innerContext&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; c.get(&lt;span&gt;"CONTEXT"&lt;/span&gt;); System.out.println(innerContext.hasKey(&lt;span&gt;"abc"&lt;/span&gt;)); &lt;span&gt;return&lt;/span&gt; Mono.empty(); })); &lt;span&gt;ServerHttpRequest&lt;/span&gt; &lt;span&gt;request&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; exchange.getRequest(); &lt;span&gt;BodyCaptureExchange&lt;/span&gt; &lt;span&gt;bodyCaptureExchange&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;BodyCaptureExchange&lt;/span&gt;(exchange); &lt;span&gt;return&lt;/span&gt; chain.filter(bodyCaptureExchange).subscriberContext(context -&gt; { &lt;span&gt;//HERE I PUT CONTEXT TO ITSELF&lt;/span&gt; context = context.put(&lt;span&gt;"CONTEXT"&lt;/span&gt;, context); &lt;span&gt;return&lt;/span&gt; context; }); } }&lt;/code&gt; Here is my controller method:</pre> <pre class="lang-java s-code-block">&lt;code&gt;&lt;span&gt;public&lt;/span&gt; Mono&lt;ResponseEntity&lt;Response&gt;&gt; &lt;span&gt;post&lt;/span&gt;&lt;span&gt;(&lt;span&gt;@RequestBody&lt;/span&gt; Mono&lt;Request&gt; request)&lt;/span&gt; { Mono&lt;ResponseEntity&lt;Response&gt;&gt; responseEntityMono = request.flatMap( rq -&gt; service.requestProcess(rq).map(res -&gt; ResponseEntity.ok().body(res)) ).subscriberContext(c -&gt; { &lt;span&gt;//HERE I GET THE INNER CONTEXT FROM ABOVE FILTER&lt;/span&gt; &lt;span&gt;Context&lt;/span&gt; &lt;span&gt;innerContext&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; c.get(&lt;span&gt;"CONTEXT"&lt;/span&gt;); &lt;span&gt;//PUT DATA TO IT&lt;/span&gt; c = c.put(&lt;span&gt;"CONTEXT"&lt;/span&gt;, innerContext.put(&lt;span&gt;"abc"&lt;/span&gt;, &lt;span&gt;"abc"&lt;/span&gt;)); &lt;span&gt;//ALSO PUT DATA TO PARENT&lt;/span&gt; c = c.put(&lt;span&gt;"abc"&lt;/span&gt;, &lt;span&gt;"abc"&lt;/span&gt;); &lt;span&gt;return&lt;/span&gt; c; }); &lt;span&gt;return&lt;/span&gt; responseEntityMono; }&lt;/code&gt; </pre> <p> The System.out.println in RequestFilter print: </p> <pre class="lang-java s-code-block">&lt;code&gt;&lt;span&gt;false&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;/code&gt;</pre> <p> Means that data I put from controller are lost when it come to Filter.  Please correct me if I'm wrong somewhere. I really need your help </p> <p> </p> <p> Reactor version: 3.3.14 </p> <p> </p> <p> Spring boot stater webflux: 2.3.9.RELEASE </p> <p> </p> </div> </div>
Answer