Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Query Parameters on a Jersey Test Call

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?

like image 666
kwiqsilver Avatar asked Jul 15 '14 23:07

kwiqsilver


People also ask

How do I give a parameter query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

Can URI have query parameters?

It is very important to know when to use Query Parameter or URI Parameter while designing an API. URI parameter (Path Param) is basically used to identify a specific resource or resources whereas Query Parameter is used to sort/filter those resources.


1 Answers

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()
like image 56
Michal Gajdos Avatar answered Oct 07 '22 05:10

Michal Gajdos