StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
ServerWebExchange.getAttributes().put("KEY", "VALUE")
<code><span>@Component</span> <span>public</span> <span>class</span> <span>RequestFilter</span> <span>implements</span> <span>WebFilter</span> { <span>@Override</span> <span>public</span> Mono<Void> <span>filter</span><span>(ServerWebExchange exchange, WebFilterChain chain)</span> { exchange.getResponse().beforeCommit(() -> Mono.deferWithContext(c -> { <span>//I CHECK WHETHER DATA SHARED FROM CONTROLLER EXIST OR NOT</span> System.out.println(c.hasKey(<span>"abc"</span>)); <span>Context</span> <span>innerContext</span> <span>=</span> c.get(<span>"CONTEXT"</span>); System.out.println(innerContext.hasKey(<span>"abc"</span>)); <span>return</span> Mono.empty(); })); <span>ServerHttpRequest</span> <span>request</span> <span>=</span> exchange.getRequest(); <span>BodyCaptureExchange</span> <span>bodyCaptureExchange</span> <span>=</span> <span>new</span> <span>BodyCaptureExchange</span>(exchange); <span>return</span> chain.filter(bodyCaptureExchange).subscriberContext(context -> { <span>//HERE I PUT CONTEXT TO ITSELF</span> context = context.put(<span>"CONTEXT"</span>, context); <span>return</span> context; }); } }</code> Here is my controller method:
<code><span>public</span> Mono<ResponseEntity<Response>> <span>post</span><span>(<span>@RequestBody</span> Mono<Request> request)</span> { Mono<ResponseEntity<Response>> responseEntityMono = request.flatMap( rq -> service.requestProcess(rq).map(res -> ResponseEntity.ok().body(res)) ).subscriberContext(c -> { <span>//HERE I GET THE INNER CONTEXT FROM ABOVE FILTER</span> <span>Context</span> <span>innerContext</span> <span>=</span> c.get(<span>"CONTEXT"</span>); <span>//PUT DATA TO IT</span> c = c.put(<span>"CONTEXT"</span>, innerContext.put(<span>"abc"</span>, <span>"abc"</span>)); <span>//ALSO PUT DATA TO PARENT</span> c = c.put(<span>"abc"</span>, <span>"abc"</span>); <span>return</span> c; }); <span>return</span> responseEntityMono; }</code>
<code><span>false</span> <span>false</span> </code>
// RequestFilter</span> <span>// exchange.getResponse().beforeCommit</span> <span>Map</span><<span>Object</span>, <span>Object</span>> innerContext = c.<span>get</span>(<span>"CONTEXT"</span>); <span>// return chain.filter(exchange)</span> <span>Map</span><<span>Object</span>, <span>Object</span>> innerContext = <span>new</span> HashMap<>(); context = context.put(<span>"CONTEXT"</span>, innerContext); <span>return</span> context; <span>// controller</span> <span>Map</span><<span>Object</span>, <span>Object</span>> innerContext = c.<span>get</span>(<span>"CONTEXT"</span>); innerContext.put(<span>"abc"</span>, <span>"abc"</span>); <span>//PUT DATA TO IT</span> c = c.put(<span>"abc"</span>, <span>"abc"</span>); <span>return</span> c;
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Your account is signed in on the device(s) below. Sign out of at least one of them to continue signing in on this device.
Your account is signed in on the device(s) below. Sign out directly on one of those devices, or wait until the sign-in again time shown below.
If you cant access those devices, please contact the administrator to sign them out for you.
Input your email to receive reset password link, or if you remember your password, you can click