Missing Dependencies for HttpServletRequest with Jersey

I have a standalone jersey server running at the beginning of my JunitTest. I am testing if my JaxRS controller is working as well as my custom HttpClient. Note that I could always use this JaxRsResourceController built-in to the glass fish.

Here is the JaxRsController (light version)

@Path("root") public class JaxRsResourceController implements ResourceController<HttpServletRequest> { @Context private UriInfo context; @Context HttpServletRequest request; @Context HttpServletResponse response; @GET public String hello(){ System.out.println("Uri is "+this.context.getBaseUri().toString()); return "Hello "+peoples; } } 

I have no problems with the client, but when I start the server, I have:

 GRAVE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Missing dependency for field: javax.servlet.http.HttpServletRequest com.robustaweb.library.rest.controller.implementation.JaxRsResourceController.request SEVERE: Missing dependency for field: javax.servlet.http.HttpServletResponse com.robustaweb.library.rest.controller.implementation.JaxRsResourceController.response at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:172) at com.robustaweb.library.rest.server.JerseyServer.startServer(JerseyServer.java:44) 

This basically means that during the insertion time of @Context there is no dependency on HttpServletRequest. However, if I delete @Context annotations by request and response, but save it for the UriInfo context , this is normal and I can read the Uri.

I changed Maven pom several times, which now needs to force libs:

 <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.14</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> </dependencies> 

Any idea?

+6
source share
2 answers

It was not easy, but I found out. The thing is, in my JUnit test, I created the server as follows:

 HttpServer server = HttpServerFactory.create(url); 

But in this case, you create a lightweight container in which there are no servlet containers, and the reason for the failure. Therefore, to get all this, I used a jersey-test framework that allows you to use the Grizzly web container (or even the built-in glass fish).

Here is the maven:

 <dependencies> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <!-- Unit test are using jersey server directly --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.jersey.test.framework</groupId> <artifactId>jersey-test-framework</artifactId> <version>1.0.3</version> <scope>test</scope> </dependency> </dependencies> 

Here is an example of a JerseyServerTest: note that it extends the JerseyTest

 public class JerseyServerTest extends JerseyTest { protected String baseUri = "http://localhost:" + TestConstants.JERSEY_HTTP_PORT + "/"; public JerseyServerTest() throws Exception { super("com.robustaweb.library.rest.controller"); /* It possible to NOT call the super() but to manually do : 1) ApplicationDescriptor appDescriptor = new ApplicationDescriptor() .setRootResourcePackageName(resourcePackageName) // resource packages .setContextPath(contextPath) //context of app .setServletPath(servletPath); // context of spi servlet 2)setupTestEnvironment(appDescriptor); */ } @Test public void testHelloWorldRequest() { SunRestClient client = new SunRestClient(baseUri + "root"); String result = client.GET("", null); System.out.println(result); } @Test public void testDeleteRequest() { SunRestClient client = new SunRestClient(baseUri + "root"); String result = client.DELETE("john", null); System.out.println(result); } } 

And finally, a resource file containing @GET and @DELETE

 @Path("root") public class JaxRsController extends JaxRsResourceController{ List<String> peoples = new ArrayList<String>(); @GET public String hello(){ System.out.println("Uri is "+getUri()); return "Hello "+peoples; } @DELETE @Path("{name}") public String deletePeople(@PathParam("name") String name){ System.out.println("deleting "+name); this.peoples.remove(name); return String.valueOf(peoples.size()); } } 

And now it works! I got some help in this article , and there is a small chapter in the documentation . Beeing, capable of attaching the source code for the Jersey framework, really helped, so he also wants IntelliJ.

+1
source

servlet dependencies were split into another module, try adding

 <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.14</version> </dependency> 

to your pom.

+1
source