注入的 ServetContext 在 Grizzly 上为 null,在 TomCat 7 中不为 null
我正在开发 Jax-RS RESTful Web 服务。虽然我仍在编码(在 Eclipse 中),但我希望能够轻松调试,因此我决定创建一个 Maven 项目,并使用 Grizzly Web 服务器的依赖项,这样我只需 2代码行(甚至不必构建 WAR 文件)。
在我的 Web 服务实现类(带有 @Path 的类)中,我已将上下文作为成员变量注入:
@javax.ws.rs.core.Context
ServletContext context;
当我检查上下文变量是否为 null 时,如果我构建 WAR 文件并将其部署到 TomCat 中,它就不会为 null,但是当我启动 Grizzly 服务器并检查它时,它将为空。启动 Grizzly v1.9 服务器的过程如下:
String url = "http://localhost:1234";
SelectorThread srv = GrizzlyServerFactory.create(url);
我尝试通过 Google 找到解决方案,有人建议在 web.xml 中启用“load-on-startup”,但这也没有帮助。
有什么想法吗? 干杯!
I'm working on a Jax-RS RESTful web-service. While I'm still coding (in Eclipse), I'd like to be able to debug easily, so I decided to have a Maven project and I use dependencies to the Grizzly web server, allowing me to start up a server with merely 2 lines of code (not even having to build the WAR file).
In my web-service implementation class (the one with @Path) I have injected the context as member variable:
@javax.ws.rs.core.Context
ServletContext context;
When I check the context variable for null, it will not be null if I build the WAR file and deploy it in TomCat, but it will be null when I start my Grizzly server and check it then. Starting the Grizzly v1.9 server is done as follows:
String url = "http://localhost:1234";
SelectorThread srv = GrizzlyServerFactory.create(url);
I've tried to find solutions to this with Google, someone suggested to enable "load-on-startup" in the web.xml, but this didn't help either.
Any ideas?
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是您使用的是没有 Servlet 容器扩展的 Grizzly。理论上JAX-RS可能不仅运行在servlet环境中,但是这样你就无法获取ServletContext或HttpServletRequest或Response。
My guess is that you use Grizzly without the Servlet Container extension. Theoretically JAX-RS may run not only on servlet environment, but then you cannot get the ServletContext or HttpServletRequest or Response.
虽然这里的其他答案有一定道理,但我不太确定,因为根据 Maven 依赖关系树,还包含了 servlet 包。
我可以通过迁移到不同的 Maven 依赖项来解决问题。我现在正在使用
并遵循 http://blogs.oracle.com/PavelBucek/entry 的说明/jersey_grizzly_2_x_integration(示例)以使其正常工作。 servlet 上下文现已成功注入。
While the other answer here made some sense, I wasn't too sure, because according to the maven dependency tree, the servlet package was also included.
I could fix the problem by migrating to a different maven dependency. I'm now using
and followed the instructions of http://blogs.oracle.com/PavelBucek/entry/jersey_grizzly_2_x_integration (Sample) to make it work. The servlet context is now successfully injected.