Set Query Parameters on a Jersey Test Call

grizzly, java, jersey, jersey-2.0

Solution

JavaDoc to WebTarget.queryParam() should give you an answer to your problem. Basically you need to transform your code to something like:

target("foo/bar").queryParam("key", "blah").request().get()

Problem

I have a Jersey based Java servlet: ``` @Path("foo") class Foo { @GET @Path("bar") public Response bar(@QueryParam("key") String value) { // ... } } ``` I can call it in Tomcat just fine as: ``` http://localhost:8080/container/foo/bar?key=blah ``` However, in my JerseyTest, using Grizzly, it's not handling the parameters properly. This test case returns a 404 error: ``` @Test public void testBar() { final Response response = target("foo/bar?key=blah").request().get(); } ``` I suspect the issue is it's looking for a resource named `foo/bar?key=blah` rather than trying to pass `key=blah` to the resource at `foo/bar`. If I pass just `"foo/bar"` to `target()`, I get a 500, as the code throws an exception for a null parameter. I looked through the Jersey Test documentation, and some examples, and I found some cryptic looking stuff that might have been for passing parameters to a GET, but none of it looked like it was assigning values to parameters, so I wasn't positive how I would use it. How can I pass my value in for that parameter?

Original source