ServerWebExchange属性为空 [英] ServerWebExchange attributes are empty
问题描述
我有基于WebFlux构建的基于Spring Boot注释的控制器.我实现了WebFilter
,在WebFilter
内部调用了serverWebExchange.getAttributes()
,它返回了4个属性,这很好.但是此后它停止工作,现在总是返回空的属性映射.我没有更改控制器中的任何内容,但是即使我做了更改,我仍然应该获得那些属性.为什么它停止工作?
I have Spring Boot annotation based controller built on WebFlux. I implement WebFilter
, inside WebFilter
I invoked serverWebExchange.getAttributes()
and it returned 4 attributes which is fine. But after that it stopped working and now it always returns empty attributes map. I didn't change anything in controller, but even if I did I should still get those attributes. Why it stopped working?
推荐答案
第一次使用该方法会起作用:
It works when firstly I do:
this.handlerMapping.getHandler(serverWebExchange)
其中handlerMapping
是类型为RequestMappingHandlerMapping
的bean,您可以从WebFlux注入它.
where handlerMapping
is a bean of type RequestMappingHandlerMapping
which you can inject from WebFlux.
这行代码初始化serverWebExchange
属性.有问题的是我写道它曾经工作过然后停止了.这样做的原因是当时我正在尝试handlerMapping
并使其起作用,然后我将其删除并且属性停止了初始化.调试显示,通常这些属性是在应用WebFilter
之后设置的.这行代码使它可以更早地初始化.
This line of code initializes serverWebExchange
attributes. In question I wrote that it worked once and then it stopped. The reason for that is that I was experimenting with handlerMapping
at that time and it made it work, then I removed it and attributes stopped being initialized. Debugging showed that normally those attributes are set after WebFilter
s are applied. This line of code makes it initialize earlier.
这篇关于ServerWebExchange属性为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!