A different world
In a normal
Spring Web project, it is very easy to get the
Request object and many libraries provide static methods to get it. The code to get it is as follows.
RequestContextHolder provides static methods, which means you can call it from anywhere. And it uses
ThreadLocal to hold the
Request object, which means that different threads can get their own Request objects.
But in the responsive
WebFlux world, there is no similar
Holder class provided, and
WebFlux is not thread-aware; any thread can handle any request at any time, and if it feels more efficient to switch the current thread, it will do so. But in a
Servlet Based application, it will schedule a thread for a request to handle the entire process.
This huge difference means that you can’t simply save and fetch
Save first, then get
To make it easy to get the
Request object later, we need to store it in a container with the same
scope that we can use at the beginning. Two key issues need to be addressed here.
Where does the
Requestobject come from.
Where to store the
For question (1), we can think back to when the
Request object appeared, and the easiest one to think of is the
WebFilter, which has the following method signature.
We can get the
Request object directly through the
Filter can be executed before the application logic, the requirement is satisfied and problem (1) is solved.
For problem (2), a container with the same scope as the
Reavtive request is needed. Use
A look at the official documentation for
reactor shows the following passage.
3.1.0, Reactor comes with an advanced feature that is somewhat comparable to
ThreadLocalbut can be applied to a
Monoinstead of a
Thread. This feature is called
And the official website also gives an explanation why
ThreadLocal does not work in some scenarios. Take a look if you are interested.
WebFilter fetch and save
First, get the
Request object in the
WebFilter and save it, the code is as follows.
ServerHttpRequest object from the
ServerWebExchange and put it into the
Context via the
Tool Classes - Holder
Implement a tool class to provide static methods that can be used in any scenario after
Using in Controller
We try to use
ReactiveRequestContextHolder in the
Controller to get the
The above method gets the
Request object and then gets the
Header in the
Start the application and test it as follows.
The request header
user can be successfully retrieved.