Using relative urls to query the resource

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Using relative urls to query the resource

ravthiru
This post was updated on .
I am not able to retrieve the resource using relative URL .

Page URL is http://localhost:8080/page/123
Image url is  http://localhost:8080/image/1001

While retrieving  Page i get relative urls for the images like ../image/<ImageId>

I used below code to get the images, but it ends-up with resource not found (404)

     String base = "http://localhost:8080/page";
      String path = "../image/1001";
      WebTarget target = ClientBuilder.newClient().target(base);
      target=target.path(path);
      response = target.request().get();

Final Url will be http://localhost:8080/page/../image/1001

Is this correct way to use relative URLs ?
Reply | Threaded
Open this post in threaded view
|

Re: Using relative urls

Pavel Bucek-2

Hi Ravthiru,

WebTarget#path appends the argument to the existing path, see the javadoc:

/**
 * Create a new {@code WebTarget} instance by appending path to the URI of
 * the current target instance.
...

You can always use URI/URL from java and its #resolve(...) method. (target has #getUri() method).

Jersey client is usually the expensive part, WebTarget instances are "for free", especially when you are changing only the URI. So the best practice here is to keep the Client instance stored somewhere, reuse it and create as many WebTarget instances as you want. (don't forget to call Client#close() when you are done with that instance).

Regards,
Pavel


On 06/03/2017 02:15, ravthiru wrote:
I am not able to fetch the resource using relative URL .


Page URL is http://localhost:8080/page/123
Image url is  http://localhost:8080/image/1001

While retrieving  Page i send relative urls of the images, 
later i want to retrieve images using the base url 

I used below code to get the images, but it end up with resource not found
(404)

     String base = "http://localhost:8080/page";
      String path = "../image/1001";
      WebTarget target = ClientBuilder.newClient().target(base);
      target=target.path(path);
      response = target.request().get();

Is this correct way to use relative URl



--
View this message in context: http://jersey.576304.n2.nabble.com/Using-relative-urls-tp7583941.html
Sent from the Jersey mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Using relative urls

ravthiru
This post was updated on .
Thanks Pavel for your quick reply,

now i am using resolve  to get absolute URI for the image, some thing like this

 URI imageURI = baseURI.resolve("../image/1001");

thanks for pointing me to reuse the client and use close() when done.