Injected ServetContext is null on Grizzly, not null in TomCat 7
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开发者_如何学JAVA.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!
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.
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
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>1.9.1</version>
</dependency>
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.
精彩评论