【发布时间】:2012-03-25 14:43:17
【问题描述】:
我使用 AppFuse 创建了一个基本的应用程序外壳,并按照 AppFuse tutorial 使用 Jax-RS 创建了一个简单的 RESTful 服务。这工作得很好。对 http://localhost:8080/services/api/persons 的调用将 Person 对象的集合返回为具有正确数据的 Json 格式字符串。
我现在想从 Appfuse 公开的 RESTful 服务中访问 ServletRequest 和 ServletResponse 对象(以使用需要这些对象的另一个库)。
我 认为 这应该可以通过添加 @Context 注释来实现,例如关注这个StackOverflow post 和这个forum post。
但是如果我添加@Context 标记(见下文),它可以正常编译,但在服务器重新启动时会抛出异常(附在底部)。
这是@WebService的声明:
@WebService
@Path("/persons")
public interface PersonManager extends GenericManager<Person, Long> {
@Path("/")
@GET
@Produces(MediaType.APPLICATION_JSON)
List<Person> read();
...
}
这是我认为我会调用 @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
...
}
希望我遗漏了一些简单的东西,要么包括一些使其工作的东西,要么意识到获取 ServletRequest 是不可能的,因为......欢迎任何线索。
我在 IntelliJ 的 Tomcat 上运行它。
=== 异常堆栈跟踪(截断)===
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
【问题讨论】:
标签: java rest servlets jax-rs appfuse