users@jersey.java.net

Re: [Jersey] Access to a classes _at_Path

From: Chris Carrier <ctcarrier_at_gmail.com>
Date: Fri, 5 Mar 2010 10:56:03 -0800

Thanks for the input that's definitely on the right path. Though it
doesn't quite seem to be generating the path's i'm after. I tried
something like:

@GET
    @Path("/{key}")
    public Response retrieve(@PathParam("key") Long key, @Context
UriInfo uriInfo) throws HttpInternalErrorException,
HttpNotFoundException {

        Response result = super.retrieve(key);

        PersonAddressMapping personAddressMapping =
(PersonAddressMapping)result.getEntity();

        URI personResource =
uriInfo.getRequestUriBuilder().path(PersonEndpoint.class,
"retrieve").build(personAddressMapping.getPersonId());
        URI addressResource =
uriInfo.getRequestUriBuilder().path(AddressEndpoint.class,
"retrieve").build(personAddressMapping.getAddressId());

        personAddressMapping.setPersonResource(personResource);
        personAddressMapping.setAddressResource(addressResource);

        return result;
    }

But the URL's are relative to my mapping Path so they're like:

http://www.something.com/personAddressMapping/123/456

Where 123 is the personAddressMapping key and 456 is the person key.
I've tried getting the URI builder via getRequestUriBuilder,
getAbsolutePathBuilder, and getBaseUriBuilder but none of them
generate a URL like:

http://www.something.com/person/456

Am I getting the UriBuilder in the wrong way?

Thanks!
Chris


On Fri, Mar 5, 2010 at 10:17 AM, Moises Lejter <moilejter_at_gmail.com> wrote:
> I think you want to use a UriBuilder.
> The example I have looks like this:
> private String buildRef(Airport a, UriBuilder ub) {
>   URI result = ub.path(AirportRM.class,"getByCode").build(a.getCode());
>   return result.toString();
> }
> where "AirportRM" is a resource class, and "getByCode" is one of the
> methods in it.  The URI will be built based on the URI template
> detemined by the @Path annotations for AirportRM and its method
> getByCode(), with the param expected replaced by the parameter to
> build().
> Moises
>