Spring 3 MVC accessing HttpRequest from controller

Spring MvcHttprequestHttpsessionRequest Mapping

Spring Mvc Problem Overview


I would like to handle request and session attributes myself rather then leave it to spring @SessionAttributes, for login of cookies handling for example.

I just cant figure out how could I access the HttpRequest from within a controller, I need a way to go a layer above the @RequestAttribute and access the HttpRequest itself. With Stripes in used to do this by implementing an ApplicationContext and calling getAttribute().

Also, passing the HttpServletRequest as parameter seems not to be working:

@RequestMapping(value="/") public String home(HttpServletRequest request){
    System.out.println(""+request.getSession().getCreationTime());
    return "home"; 
}

The above method does not print anything.

Do you have any advice on this?

Spring Mvc Solutions


Solution 1 - Spring Mvc

Spring MVC will give you the HttpRequest if you just add it to your controller method signature:

For instance:

/**
 * Generate a PDF report...
 */
@RequestMapping(value = "/report/{objectId}", method = RequestMethod.GET)
public @ResponseBody void generateReport(
        @PathVariable("objectId") Long objectId, 
        HttpServletRequest request, 
        HttpServletResponse response) {
    
    // ...
    // Here you can use the request and response objects like:
    // response.setContentType("application/pdf");
    // response.getOutputStream().write(...);

}

As you see, simply adding the HttpServletRequest and HttpServletResponse objects to the signature makes Spring MVC to pass those objects to your controller method. You'll want the HttpSession object too.

EDIT: It seems that HttpServletRequest/Response are not working for some people under Spring 3. Try using Spring WebRequest/WebResponse objects as Eduardo Zola pointed out.

I strongly recommend you to have a look at the list of supported arguments that Spring MVC is able to auto-magically inject to your handler methods.

Solution 2 - Spring Mvc

I know that is a old question, but...

You can also use this in your class:

@Autowired
private HttpServletRequest context;

And this will provide the current instance of HttpServletRequest for you use on your method.

Solution 3 - Spring Mvc

Another way is to use RequestContextHolder like: (Since: 2.0)

RequestContextHolder Holder class to expose the web request in the form of a thread-bound RequestAttributes object. The request will be inherited by any child threads spawned by the current thread if the inheritable flag is set to true.

HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

Solution 4 - Spring Mvc

@RequestMapping(value="/") public String home(HttpServletRequest request){
    System.out.println("My Attribute :: "+request.getAttribute("YourAttributeName"));
    return "home"; 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJBoyView Question on Stackoverflow
Solution 1 - Spring MvcjjmontesView Answer on Stackoverflow
Solution 2 - Spring MvcDeividi CavarzanView Answer on Stackoverflow
Solution 3 - Spring Mvcsendon1982View Answer on Stackoverflow
Solution 4 - Spring MvcSaiSudha HGView Answer on Stackoverflow