Spring响应式请求上下文映射未找到错误

huangapple go评论110阅读模式
英文:

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() + &quot;.EXCHANGE_CONTEXT&quot;;


	@Override
	public Mono&lt;Void&gt; filter(ServerWebExchange exchange, WebFilterChain chain) {
		return chain.filter(exchange)
				.subscriberContext(cxt -&gt; cxt.put(EXCHANGE_CONTEXT_ATTRIBUTE, exchange));
	}

	public static Optional&lt;ServerWebExchange&gt; 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.

huangapple
  • 本文由 发表于 2020年8月12日 01:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63363256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定