我创建了一个web服务,在该服务上我使用了@Stateless annotation,即
@Stateless
@Path("/boo/too")
public class RestController {
@Context
private HttpServletRequest request;
@Context
private ServletContext context;
@GET
@Path("/coo")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public GetResObj getFuncName(
@HeaderParam("foo") String foo,
@QueryParam("boo") String boo
) throws Exception{
MyClass className =(ClassCast) request.getSession().getAttribute("myClassInstance");
}
现在,根据我所做的一些阅读和这个问题的准确答案:问题
,我理解无状态对象是一个可以有变量但不可变(不能保持任何状态(的对象。当使用@Stateless注释时,代码的request.getSession()
部分抛出一个nullPointerException。当我删除@Stateless注释时,request.getSession()
工作正常。
你明白为什么会这样吗?
HttpServlet请求实例似乎从未被注入请求字段。
你能检查将HttpServlet请求注入资源方法参数列表吗?例如,最终的@Context HttpServlet请求。