Re: Re: [Jersey] can we get a UriBuilder.addOrReplaceSegment method? Tuesday, February 2, 2010 11:51 PM

From: Craig McClanahan <>
Date: Thu, 4 Feb 2010 19:21:49 -0800

On Thu, Feb 4, 2010 at 6:45 PM, geoffrey hendrey <> wrote:
> Hi Craig,
> I don't want a new view of the same resource. For example, consider a URI
> like  "/page/5". The resource for page 5 wants to be able to create links to
> /page/4 and /page/6, it is extremely useful to be able to say
> addOrReplaceSegment("/page/{page}", "6"). I can even envision a syntax like:
> addOrReplaceSegment("/page/{page}", "${page}+1") but that's above and beyond
> what I need.
> I hope I won't see a response saying "page numbers should be query
> parameters" since at that point the issue drifts toward the religious rather
> than the practical. There are many reasons, such as availability of matrix
> parameters to the path segments, or behaviors of web caches,  that make path
> parameters preferable in some situations. I just want an easy way to take a
> resource's URI and mutate its path parameters so that I can easily create
> links to similar resource. So far, virtually everything else is a dream with
> Jersey and JSR-311, but this is one place where I think it could be
> improved.

It's too bad you don't like query parameters for this ... the
queryParam() method on UriBuilder would be just the ticket :-).

A not-recommended alternative would be to start from
UriInfo.getBaseUriBuilder() and build the whole thing up from scratch.
 That would be spreading knowledge of how your uris are constructed
all over the place.

A better option might be to consider
getBaseUriBuilder().path(MyResourceClass.class) that will give you the
path matching the @Path declaration on your current resource class.
Then, you only have to add path elements for the last part.

If you really want to start from getAbsoluteUriBuilder(),assume you're
on page 5 with a path like:


If you add path segments ".." and "6" (or, more generally, the page
number you want to reference as the second segment), you should get
where you want to be.


> Cheers,
> geoff
> --
> - RESTful Relational Database