I have a WebFilter that injects a context
public class MyWebFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange)
.contextWrite(ctx -> ctx.put("context", "some-context"));
}
}
In my Controller, I can read it and return it successfully
@RestController
public class MyController {
@GetMapping(path = "test")
public Mono<String> test() {
return Mono.deferContextual(ctx -> Mono.just(ctx.get("context")));
}
}
However, if I call .block() on it:
Mono.deferContextual(ctx -> Mono.just(ctx.get("context"))).block();
it throws this exception
java.util.NoSuchElementException: Context does not contain key: context
I'm having trouble wrapping my head around the order of execution in the reactive world.
non-blocking framework?blockyou are breaking the reactive pipeline/chain and the context can't be propagated that way. Context is propgated through subscription..block()method available and when is it allowed then?