if the request is made, the value is missing in the reactive context (Context is empty).
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class ReactiveRequestContextFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
return chain.filter(exchange)
.contextWrite(ctx -> ctx.put("ServerHttpRequest.class", request));
}
}
public class ReactiveRequestContextHolder {
public static Mono<ServerHttpRequest> getRequest() {
return Mono.deferContextual(ctx ->
{
// -------------------------------------------
// reactor.core.Exceptions$ErrorCallbackNotImplemented: java.util.NoSuchElementException: Context is empty
// -------------------------------------------
return Mono.just(ctx.get("ServerHttpRequest.class"));
});
}
}
@GetMapping(value = "/test")
public Mono<String> get() {
Mono<ServerHttpRequest> req = ReactiveRequestContextHolder.getRequest();
req.flatMap(request -> {
HttpHeaders headers = request.getHeaders();
return Mono.just(headers.getFirst("user"));
}).subscribe(s -> System.out.println(s));
return Mono.just("ok");
}
Comment From: bclozel
Thanks for getting in touch, but it feels like this is a question that would be better suited to StackOverflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.