33

I am using Jersey restful web services . This is my below code

@Path(/test)
public class testService  {
    @POST
    public String getData(Postdata postdata) {

    }

}

My question is , is it possible to get access to httpSession Object here in this class ??

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
Pawan
  • 31,545
  • 102
  • 256
  • 434

2 Answers2

45

Try:

@POST
public String getData(Postdata postdata, @Context HttpServletRequest request) {
  HttpSession session = request.getSession();
}
condit
  • 10,852
  • 2
  • 41
  • 60
22

If your service is NOT singleton, you can use:

@Path("/test")
public class TestResource  {

    @Context
    private HttpServletRequest request;

    @POST
    public String getData(Postdata postdata) {
        HttpSession session = request.getSession();
    }

}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Is it also possible to retrieve a HttpServletRequest at runtime? Via the ServiceLocator or some other way? – nicojs Aug 19 '14 at 12:58
  • Hi, @zwanz0r In runtime? I do not understand what you mean. Do you want to say using JAX-RS? – Paul Vargas Aug 19 '14 at 13:11
  • Well.. i have a spring injected bean, i.e. SessionUtil, in which i want to access the HttpSession. I don't want to be passing through the jersey injected HttpServletRequest every time I access the session util. – nicojs Aug 19 '14 at 13:40
  • OK. Then @zwanz0r you may want to see http://stackoverflow.com/questions/1629211/how-do-i-get-the-session-object-in-spring :) – Paul Vargas Aug 19 '14 at 13:41
  • Thanks! It didn't work at first because I got this error: http://stackoverflow.com/questions/2039522/getting-a-no-thread-bound-request-found-error-from-spring-in-my-web-app. After I added the listener class RequestContextListener, it worked. Now I can also let spring inject the request. Just 2 questions: - Can I simply add a second listener class without consequences? - Can I let spring inject the servlet request? As the spring bean is a singleton and request should obviously not be a singleton (when i use reflection is see that the actual class is a proxy, which is hopeful) – nicojs Aug 19 '14 at 14:05
  • After a few tests and reading documentation i found that it is actualy save. Sorry for my 'stupid' questions, i'm still new in the java world :). Thanks for the help! – nicojs Aug 19 '14 at 14:29
  • @PaulVargas: I can easily get request object in resource class, But I don't want to pass that request object to subsequent method each time, So my question is How can we get request object in any normal java class(Any helper, wrapper class)? I am using (struts 2 + jersey) – HybrisHelp Jan 12 '15 at 08:15
  • 1
    @U2Answer You can try with `ThreadLocal`, see http://stackoverflow.com/a/5473312/870248 – Paul Vargas Jan 12 '15 at 08:22
  • Thanks!, Is there any ready Jersey Implementation class which tackle this ThreadLocal etc etc and help to get current request object where we want. – HybrisHelp Jan 12 '15 at 09:38
  • 1
    @U2Answer Humm... nope. See `https://github.com/jersey/jersey/search?utf8=✓&q=ThreadLocal&type=Code` – Paul Vargas Jan 12 '15 at 09:44
  • Thanks! So i have to manually store request in ThreadLocal from ContainerRequestFilter – HybrisHelp Jan 12 '15 at 10:00
  • Paul, can you please answer my question about sessions and RESTs. Is not it that rests are stateless, and we must not have sessions on it? Even if RESTs are based on servlets should we have opportunity to have an access to session object? – Dante Sep 09 '16 at 08:51
  • @Dante Yes! You can access to the session object. BTW, what is your question? – Paul Vargas Sep 09 '16 at 12:37
  • Paul, please follow the link: http://stackoverflow.com/questions/7874695/servlet-vs-restful First answer, author said: "In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions)." And you are telling that get access to sessions - is ok, so i am little confused to be honest – Dante Sep 09 '16 at 14:59
  • 1
    By ***design***, yes!, it must be stateless. ***But***, since JAX-RS is servlet-based, you can access to the session object, like a normal servlet, using the `request` object. You can do things like [this](https://gist.github.com/paul-vargas/d693b9a09645db1e6b9d000a6100f4ba). Awesome! – Paul Vargas Sep 09 '16 at 15:22
  • How can we know that is my service singleton or not? – kodmanyagha Oct 06 '16 at 19:31