ServerWebExchange属性为空 [英] ServerWebExchange attributes are empty

查看:969
本文介绍了ServerWebExchange属性为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于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 WebFilters are applied. This line of code makes it initialize earlier.

这篇关于ServerWebExchange属性为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆