Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PathParam for Jersey WebResource

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&param2=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?

like image 249
Jane Avatar asked Jun 13 '16 19:06

Jane


People also ask

What is the difference between PathParam and QueryParam?

@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.

What is @PathParam?

@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.


1 Answers

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");
like image 152
sisyphus Avatar answered Oct 06 '22 17:10

sisyphus