在Jax Rs / Appfuse应用程序中获取HttpServletRequest? [英] Get HttpServletRequest in Jax Rs / Appfuse application?
问题描述
我使用AppFuse创建了一个基本的应用程序shell,并遵循了 AppFuse教程使用Jax-RS创建一个简单的RESTful服务。这很好用。对 http:// localhost:8080 / services / api / persons
的调用将Person对象的集合作为带有正确数据的Json格式字符串返回。
I created a basic application shell with AppFuse, and followed the AppFuse tutorial to create a a simple RESTful service with Jax-RS. That works just fine. A call to http://localhost:8080/services/api/persons
returns a collection of Person objects as a Json formatted string with the right data.
我现在想要访问 ServletRequest
和 ServletResponse
Appfuse公开的RESTful服务中的对象(使用另一个需要这些对象的库)。
I now would like to access the ServletRequest
and ServletResponse
objects from within a RESTful service exposed by Appfuse (to use another library that requires these objects).
我认为应该通过添加@Context注释来实现,例如按照 StackOverflow帖子和论坛帖子。
I think that should be doable by adding an @Context annotation, e.g. following this StackOverflow post and this forum post.
但是如果我添加@Context标签(如下所示,它可以很好地编译,但在重新启动服务器时会抛出异常(附在底部)。
But if I add the @Context tag (see below), it compiles fine but throws an Exception when the server is restarted (attached at bottom).
这是 @WebService
的声明:
@WebService
@Path("/persons")
public interface PersonManager extends GenericManager<Person, Long> {
@Path("/")
@GET
@Produces(MediaType.APPLICATION_JSON)
List<Person> read();
...
}
这是我认为的实施课程将调用 @Context
注释:
And here's the implementation class where I think I would call the @Context
annotation:
@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成为可能因为......任何线索都会受到欢迎。
Hopefully I'm missing something simple, either something to include to make it work, or to realize that getting the ServletRequest isn't supposed to be possible because.... Any clues would be welcome.
我在IntelliJ的Tomcat上运行它。
Am running this on Tomcat in IntelliJ.
=== EXCEPTION STACK TRACE(截断)===
=== EXCEPTION STACK TRACE (Truncated) ===
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
推荐答案
尝试注入 HttpServletRequest
和 HttpServletContext
直接:
@Context private HttpServletRequest servletRequest;
@Context private HttpServletContext servletContext;
这篇关于在Jax Rs / Appfuse应用程序中获取HttpServletRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!