I'm working on a task to create a Jersey client. I'm using Jersey 1.18. The target URL looks like below.
https://api.test.com/test/{id}?param1=test1¶m2=test2
I need to add a PathParam
to my WebResource
to call this URL. I see an option to add the QueryParam
but not for PathParam
. My code looks something like this.
Client client = Client.create();
WebResource webResource = client.resource("https://api.test.com/test/{id}")
.queryParam("param1", "test1")
.queryParam("param2", "test2");
Can anyone please help me with this?
@QueryParam is used to access key/value pairs in the query string of the URL (the part after the ?). For example in the url http://example.com?q=searchterm , you can use @QueryParam("q") to get the value of q . @PathParam is used to match a part of the URL as a parameter.
@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.
You need the path
method from WebResource
...
final String myId = "1234";
Client client = Client.create();
WebResource webResource = client.resource("https://api.test.com/test")
.path(myId)
.queryParam("param1", "test1")
.queryParam("param2", "test2");
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