英文:
Spring Reactive Request Context Map Not Found Error
问题
I'm using the ServerWebExchangeContextFilter filter to store the current ServerWebExchange context inside the context map provided by spring.
But when I try to get this saved ServerWebExchange context from a different part of my code (downstream), I get an empty context from the context map against the key EXCHANGE_CONTEXT_ATTRIBUTE
.
Context ctx = Mono.subscriberContext()
.map(ServerWebExchangeContextFilter::get)
assert ctx.isPresent()==true // This assert always fails
The ServerWebExchangeContextFitler class has the following definition:
public class ServerWebExchangeContextFilter implements WebFilter {
public static final String EXCHANGE_CONTEXT_ATTRIBUTE =
ServerWebExchangeContextFilter.class.getName() + ".EXCHANGE_CONTEXT";
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange)
.subscriberContext(cxt -> cxt.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange));
}
public static Optional<ServerWebExchange> get(Context context) {
return context.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
}
}
I am writing a common library function for my organization and would hence like to avoid passing the ServerHttpRequest inside the argument tuple. Hence, please post an answer that would allow me to access the context from a static function.
英文:
I'm using the ServerWebExchangeContextFilter filter to store the current ServerWebExchange context inside the context map provided by spring.
But when I try to get this saved ServerWebExchange context from a different part of my code (downstream), I get an empty context from the context map against the key EXCHANGE_CONTEXT_ATTRIBUTE
.
Context ctx = Mono.subscriberContext()
.map(ServerWebExchangeContextFilter::get)
assert ctx.isPresent()==true // This assert always fails
The ServerWebExchangeContextFitler class has the following definition:
public class ServerWebExchangeContextFilter implements WebFilter {
public static final String EXCHANGE_CONTEXT_ATTRIBUTE =
ServerWebExchangeContextFilter.class.getName() + ".EXCHANGE_CONTEXT";
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange)
.subscriberContext(cxt -> cxt.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange));
}
public static Optional<ServerWebExchange> get(Context context) {
return context.getOrEmpty(EXCHANGE_CONTEXT_ATTRIBUTE);
}
}
I am writing a common library function for my organisation, and would hence like to avoid passing the ServerHttpRequest inside the argument tuple. Hence please post an answer which would allow me to access the context from a static function.
答案1
得分: 1
我找到了解决办法。显然,我试图从一个隔离的函数中获取保存的上下文。
通过将这个Mono添加到我的原始控制器的执行链中,我能够解决这个问题。
这是因为ServerWebExchangeContextFilter将上下文保存在我的控制器执行链中。现在,由于我想要添加一个库方法来获取请求上下文,我不得不使用flatmap将这个函数链到我的控制器。
英文:
I found the solution for this. Apparently, I was trying to fetch the saved context from an isolated function.
By adding this Mono to my original controller's execution chain, I was able to resolve it.
This happened because the ServerWebExchangeContextFilter is saving the context inside the execution chain of my controller. Now, since I wanted to add a library method which fetches the request context, I had to chain this function to my controller using a flatmap.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论