4

Using Jersey in Java I have a response object

Client c=Client.create();
WebResource r = c.resource("http://example.com/path");

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.add("param1", value);

r=r.path(getQualifiersByPromoServicePath).queryParams(params);

ClientResponse response = r.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);

How can I get the url used to make the request? (for debug purposes) i.e. i want to get the string "http://example.com/path?param1=value" ?

epeleg
  • 10,347
  • 17
  • 101
  • 151
  • Maybe I was not clear - I am asking about a request I am building. not the one made to a service I am writing. – epeleg Jan 24 '13 at 13:48
  • Well, in that case, I'm a little confused about the question; after all, don't you require the URL in order to perform the request? In this instance it would be ``http://example.com/path``. – asthasr Jan 24 '13 at 14:02
  • The example (as always) is a simplification. parts of the URL are parameters and there are also the query parameters... Of course I can build it again from the same data I use to create the web resource and params collection. But I expected this to be somehow available in a simpler way. – epeleg Jan 24 '13 at 14:52
  • Have you tried the method ``.getURI()`` of the WebResource class? – asthasr Jan 24 '13 at 15:58
  • o.k. guys, first I would like to thank you both. Although I have stated that I am looking for "a direct answer to how do I get this URL String in code" I am going to accept azraelAT 's answer because is is proved to be SO HELPFUL for me showing what I wanted and so much more. among my reasons for this are lower rep he has, my true belief that anyone reaching this Q will benefit from it even more then from the exact answer provided by @condit and the fact that it feels to me more difficult to find. – epeleg Jan 30 '13 at 08:50

2 Answers2

8

Have you tried r.getURI();? Should be what you're looking for...

condit
  • 10,852
  • 2
  • 41
  • 60
  • 1
    This is a correct answer to the Q. however I suggest everyone to also try the solution provided by @azraelAT as it proved very helpful to me. – epeleg Jan 30 '13 at 08:51
  • Does getURI() returns the query parameters? – neves Aug 04 '17 at 18:04
2

Use a Logging Filter and direct it's output to System.out for debugging purposes.

Right after instantiating your client add the following line:

c.addFilter(new LoggingFilter(System.out));
azraelAT
  • 752
  • 5
  • 9
  • Nice and I will approve this if I don't get a direct answer to how do I get this URL String in code. – epeleg Jan 24 '13 at 14:50
  • Although not an exact answer to my Q I accped this on as it does log the URL and provide additional information (logging for the entire request and resopnse). If you are looking to just get the URI like I stated on my Q - have a look at the answer by @condit. – epeleg Jan 30 '13 at 08:53