我在理解为什么@Context
依赖注入返回 $Proxy(random number) 实例而不是 HttpServletRequest 或 HttpServletResponse 的集合时遇到了一些问题。
我正在使用 Glassfish 3.1.2.2 和它的 Jersey(Jersey: 1.11.1) 版本,我的应用程序是作为 EAR 应用程序构建的。
我有简单的@Remote 接口,我在其中注释我的方法,REST 服务没有任何问题,但是当我尝试访问 HttpServletRequest 信息时,它只会导致问题。
我在会话 bean 中注释了私有字段:
@Context
private HttpServletRequest request;
@Context
private HttpServletResponse response;
并且还创建了方法签名以包含 @Context 作为参数集:
@POST
@Path("authenticate")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response authenticate(@FormParam("username") String username, @FormParam("password") String password, @Context HttpServletRequest request, @Context HttpServletResponse response);
当我尝试在输入后调试该方法时,我可以看到全局请求和响应对象为空,而本地方法实例为 $ProxyXXX 类型。
问题是我无法访问(或者我不确定如何访问)这些对象。根据网络上的教程,我应该可以使用它们,但是当我尝试访问这些时,就会抛出:
WARNING: StandardWrapperValve[RestDataService]: PWC1406: Servlet.service() for servlet RestDataService threw exception
java.lang.IllegalStateException: No thread local value in scope for proxy of class $Proxy245
at com.sun.jersey.server.impl.ThreadLocalInvoker.invoke(ThreadLocalInvoker.java:93)
at $Proxy245.getContextPath(Unknown Source)
这就是我试图调用它们的方式(在这个例子中,我只是调用 getContextPath)
@Override
public Response authenticate(String username, String password, HttpServletRequest request, HttpServletResponse response) {
System.out.println("just a test: " + request.getContextPath());
我在这里想念什么?有没有人遇到过类似的问题?
格雷格