126

I'm trying to set up a request-scoped bean in Spring.

I've successfully set it up so the bean is created once per request. Now, it needs to access the HttpServletRequest object.

Since the bean is created once per request, I figure the container can easily inject the request object in my bean. How can I do that ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Leonel
  • 28,541
  • 26
  • 76
  • 103

3 Answers3

162

Spring exposes the current HttpServletRequest object (as well as the current HttpSession object) through a wrapper object of type ServletRequestAttributes. This wrapper object is bound to ThreadLocal and is obtained by calling the static method RequestContextHolder.currentRequestAttributes().

ServletRequestAttributes provides the method getRequest() to get the current request, getSession() to get the current session and other methods to get the attributes stored in both the scopes. The following code, though a bit ugly, should get you the current request object anywhere in the application:

HttpServletRequest curRequest = 
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();

Note that the RequestContextHolder.currentRequestAttributes() method returns an interface and needs to be typecasted to ServletRequestAttributes that implements the interface.


Spring Javadoc: RequestContextHolder | ServletRequestAttributes

samitgaur
  • 5,601
  • 2
  • 29
  • 33
  • 9
    Injection is good resolution, but I have found problems with MockMvc testing Spring validators which inject HttpServletRequest. So if you want both mock tests and production code running properly this should be the choice. – Neyko Feb 25 '13 at 14:53
  • @Neyko Why is it? for mock test(Unit test?), you can inject mock HttpServletRequest or whatever you want.. isn't it? Or you can use MockHttpServletRequest I guess? – wonhee Aug 25 '16 at 22:01
  • Is it safe to be used in a singleton-scoped service instance? – Jin Kwon Nov 22 '18 at 08:19
149

Request-scoped beans can be autowired with the request object.

private @Autowired HttpServletRequest request;
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 2
    Is there an old fashioned XML way for this? – cherouvim Aug 20 '10 at 10:45
  • 1
    @cherouvim: I don't think so, no. The current request object is not visible as a bean reference. – skaffman Aug 20 '10 at 10:49
  • 2
    Doesn't work for me (spring mvc 3.1) - maybe there is something more that needs to be done? Going with Samit's solution. – kldavis4 Dec 07 '12 at 17:07
  • 2
    The problem is that when you test validators using MockMvc and this kind of injection you'll have problems. May be the other solution will be preferred in this case – Neyko Feb 25 '13 at 14:55
  • 30
    It is possible to autowire HttpServletRequest also into non request-scoped beans, because for HttpServletRequest Spring will generate a proxy HttpServletRequest which is aware how to get the actual instance of request. So it is safe to autowire request even if your controller is singleton scoped. – vtor Feb 20 '15 at 22:27
  • 3
    **Warning for Spring <=3.1 users** the autowiring will not work running tests. – Rubens Mariuzzo May 26 '15 at 23:06
  • `Field request in xx.xx.xx.beans.i18n required a bean of type 'javax.servlet.http.HttpServletRequest' that could not be found` – e-info128 Feb 07 '18 at 13:34
  • Note that this works in other (non-request-scoped) beans as well, but for wider-scoped beans, it's best to inject `ObjectFactory` and get the current request from that where needed. – herman Nov 08 '22 at 15:04
7

As suggested here you can also inject the HttpServletRequest as a method param, e.g.:

public MyResponseObject myApiMethod(HttpServletRequest request, ...) {
    ...
}
jgreen
  • 1,132
  • 2
  • 14
  • 18
Will
  • 6,601
  • 3
  • 31
  • 42