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

pro*_*ype 26 java rest servlets jax-rs appfuse

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

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

认为应该通过添加@Context注释来实现,例如关注此StackOverflow帖子和此论坛帖子.

但是,如果我添加@Context标记(见下文),它编译正常但在服务器重新启动时抛出异常(附在底部).

这是以下声明@WebService:

@WebService
@Path("/persons")
public interface PersonManager extends GenericManager<Person, Long> {
    @Path("/")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    List<Person> read();
    ...
}
Run Code Online (Sandbox Code Playgroud)

这里是我认为我会称之为@Context注释的实现类:

@Service("personManager") 
public class PersonManagerImpl extends GenericManagerImpl<Person, Long> 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 
    ... 
    } 
Run Code Online (Sandbox Code Playgroud)

希望我错过了一些简单的东西,要么包括要使其工作,要么意识到获得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 
Run Code Online (Sandbox Code Playgroud)

Per*_*ion 40

尝试注入HttpServletRequestHttpServletContext直接:

@Context private HttpServletRequest servletRequest;
@Context private HttpServletContext servletContext;
Run Code Online (Sandbox Code Playgroud)


pro*_*ype 28

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

@Path("/")
@GET
@Produces(MediaType.APPLICATION_JSON)
List<Person> read( @Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse) { }
Run Code Online (Sandbox Code Playgroud)