users@jersey.java.net

Re: UriBuilder

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Fri, 07 Dec 2007 15:07:17 -0500

On Dec 7, 2007, at 2:12 PM, Richard Wallace wrote:
>
> I'm trying to use the UriBuilder.fromResource(MyResource.class) to
> easily build links to other resources. To my surprise, doing
> something like the following:
>
> UriBuilder.fromResource(MyResource.class).build("resourcename");
>
> where MyResource has the @Path("/myresources/{name}") results in the
> URI "http://:-1/myresources/resourcename" to being constructed. I
> was a bit surprised by this, but being that the fromResource() and
> other UriBuilder methods that return a builder instance are static,
> they would have difficulty getting access to the ServletContext and
> ResourceConfig to be able to properly build the full uris.
>
> Is there another utility method I should be using to do this or am I
> going to have to come up with some other solution?
>
You can use UriInfo.getBaseUriBuilder() to get a builder initialized
with the base URI of the application. You can then append the path to
a specific resource using the path() method. I.e. to get an absolute
URI for the resource above do something like:

uriInfo
.getBaseUriBuilder().path(Myresource.class).build("resourcename");

Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.