在Jax Rs / Appfuse应用程序中获取HttpServletRequest?

我使用AppFuse创建了一个基本的应用程序shell,并按照AppFuse教程使用Jax-RS创建了一个简单的RESTful服务。 这很好用。 对http://localhost:8080/services/api/persons的调用将Person对象的集合作为具有正确数据的Json格式字符串返回。

我现在想要从Appfuse公开的RESTful服务中访问ServletRequestServletResponse对象(使用另一个需要这些对象的库)。

认为应该通过添加@Context注释来实现,例如,遵循此StackOverflowpost和此论坛post 。

但是如果我添加@Context标记(见下文),它编译得很好但在重新启动服务器时抛出exception(附在底部)。

这是@WebService的声明:

 @WebService @Path("/persons") public interface PersonManager extends GenericManager { @Path("/") @GET @Produces(MediaType.APPLICATION_JSON) List read(); ... } 

这是实现类,我想我会调用@Context注释:

 @Service("personManager") public class PersonManagerImpl extends GenericManagerImpl implements PersonManager { PersonDao personDao; @Context ServletRequest request; // Exception thrown on launch if this is present @Context ServletContext context; // Exception thrown on launch of this is present ... } 

希望我错过了一些简单的东西,要么包含一些东西以使其工作,要么意识到获得ServletRequest是不可能的,因为……任何线索都是受欢迎的。

我在IntelliJ中的Tomcat上运行它。

=== EXCEPTION STACK TRACE(截断)===

 Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'serviceBeans' threw exception; nested exception is java.lang.RuntimeException: java.lang.NullPointerException at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358) ... 37 more 

尝试直接注入HttpServletRequestHttpServletContext

 @Context private HttpServletRequest servletRequest; @Context private HttpServletContext servletContext; 

将其添加到方法签名工作。 我想这是因为当实例化类时,请求和响应对象尚不存在,但是当由brower调用时。

 @Path("/") @GET @Produces(MediaType.APPLICATION_JSON) List read( @Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse) { }