mpmp
mpmp

Reputation: 2459

Jersey 2.22: How do I retrieve the URL (with QueryParams) from WebTarget?

Assume that:

WebTarget target = client.target("http://www.someurl.com");

target.queryParam("id", "1").request().post();

And when a request is done on that target, how do I get the full URL which is http://www.someurl.com?id=1 using the Jersey api?

Upvotes: 1

Views: 4097

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

The thing is, if you look at the javadoc for WebTarget, you will see that most of the method calls on WebTarget return a new WebTarget instance. So when you do

WebTarget target = client.target("http://www.someurl.com");
target.queryParam("id", "1").request().post();
System.out.println(target.getUri());

The target instance is not the same instance that the query parameter is added to. So you would need to either do

WebTarget target = client.target("http://www.someurl.com");
WebTarget newTarget = target.queryParam("id", "1");
newTarget.request().post();
System.out.println(newTarget.getUri());

Or

WebTarget target = client.target("http://www.someurl.com").queryParam("id", "1");
target.request().post();
System.out.println(target.getUri());

Upvotes: 2

Related Questions