users@jersey.java.net

Re: Using _at_QueryParam, using UriBuilder

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 18 Mar 2008 16:09:03 +0100

James Weir wrote:
>> If you are using URI builder you need to do this:
>>
>> URI qUri = UriBuilder.fromUri(uri).queryParam("id", id).build();
>>
>> Paul.
>>
> thats what I had originally actually which provides the following URI:
>
> http://127.0.0.1:9998/users/jim/appliances?id=1
>
> which goes back to my original email :(
>
> In order for it to work I need to create the following URI:
> http://127.0.0.1:9998/users/jim/appliances/?id=1
>
> ie an extra "/" between "appliances" and the "?"
>
> without Redirect (as you specified) the URI calls the wrong method on
> the server side.

The problem with the unexpected method call was because you embedded the
query component of the URI as a path segment.

If you do this:

   UriBuilder.fromUri(uri).path("/").queryParam("id", id).build();

it should work.

If you do this:

   UriBuilder.fromUri(uri).queryParam("id", id).build();

You should get a 404.

If you do this:

   UriBuilder.fromUri(uri).path("/?id=1").build();

then the "getItem" method will get called with an unexpected "id" path
param, "?id=1".

If you do this:

   UriBuilder.fromUri(uri).path("?id=1").build();

You should get the previous result.



> If Redirect is switched off (by default) then the
> UriBuilder in essence is creating the wrong URIs when using
> queryParam(). Its a small inconsistency with an easy workaround, but
> (again) this is causing confusion.
>

Tis swings and roundabouts :-( since with redirection on it caused
confusion too for a number of developers.

Possible solutions:

1) enable redirection

2) remove the '/' from @Path("/users/{name}/appliances/")

3) change the URI you build from to end in a '/'

4) UriBuilder.fromUri(uri).path("/").queryParam("id", id).build();


If you find redirection useful then i recommend you turn it on.

In any case for documentation purposes i think it useful to be very
explicit about URIs that terminate in a '/' or not. From what i can tell
the convention is URIs that return representations that contain
references to hierarchically related things end in a '/'.

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109