[英]How to access http session object in jersey REST service?
Hi i want to access data to anf from http session object in rest service i have googled a lot and find that @context of javax.ws.rs.core.Context gives HttpServlet object but as it is interfaec i always get it null. 嗨,我想在REST服务中从http会话对象访问数据到anf,我在Google上搜索了很多,发现javax.ws.rs.core.Context的@context给出了HttpServlet对象,但是由于它是接口,我总是将其设为null。 my code is as following 我的代码如下
@Context
private HttpServletRequest request;
private HttpSession session = request.getSession();
@Path(value = "/listAllQuestion")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response listAllQuestion(){
int pgNo = 1;
int pgSize = 5;
IPResponse response = new IPResponse();
try {
if (session.getAttribute(IpConstants.TOPIC) != null
&& session.getAttribute(IpConstants.LEVEL) != null) {
session.removeAttribute(IpConstants.TOPIC);
session.removeAttribute(IpConstants.LEVEL);
}
session.setAttribute(IpConstants.PAGENO, pgNo);
session.setAttribute(IpConstants.PAGESIZE, pgSize);
quesList = questionService.listAllQuestion(pgNo, pgSize);
please try this: 请尝试以下操作:
Move your request.getSession() down inside your listAllQuestion() method, as so: 将您的request.getSession()移至listAllQuestion()方法内,如下所示:
public Response listAllQuestion(){
HttpSession session = request.getSession():
I don't believe your request object will be populated outside the context of an actual request handler. 我认为您的请求对象不会在实际请求处理程序的上下文之外进行填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.