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?
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With