Restful : How to get access to Httpsession inside the Service class?

久未见 提交于 2019-12-20 11:09:19

问题


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 ??


回答1:


Try:

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



回答2:


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();
    }

}


来源:https://stackoverflow.com/questions/11831042/restful-how-to-get-access-to-httpsession-inside-the-service-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!