问题
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?
回答1:
It works when firstly I do:
this.handlerMapping.getHandler(serverWebExchange)
where handlerMapping
is a bean of type RequestMappingHandlerMapping
which you can inject from WebFlux.
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.
来源:https://stackoverflow.com/questions/51838281/serverwebexchange-attributes-are-empty