users@jersey.java.net

Re: [Jersey] Access to a classes _at_Path

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Fri, 05 Mar 2010 15:05:37 -0500

On Mar 5, 2010, at 2:17 PM, Chris Carrier wrote:

> So I'm getting close. The best I've managed do far is:
>
> URI personResource =
> UriBuilder.fromResource(PersonEndpoint.class).host(uriInfo.getBaseUri().toString())
> .path(PersonEndpoint.class, "retrieve")
> .build(personAddressMapping.getPersonId());
>
> It's not pretty but it returns:
>
> //http%3A%2F%2Fadmin.localhost%3A8060%2F/something/json/person/214
>
> Which is basically what I want but the host is all escaped and i can't
> figure out how to tell it to return it not escaped. Anyone know if
> I'm on the right path here?
>
If you want to build an absolute URI the best way is to start from UriInfo like this:

@Context UriInfo uriInfo;

URI personResource = uriInfo.getBaseUriBuilder()
   .path(PersonEndpoint.class)
   .path(PersonEndpoint.class, "retrieve")
   .build(personAddressMapping.getPersonId());

HTH,
Marc.

> Chris
>
> On Fri, Mar 5, 2010 at 10:56 AM, Chris Carrier <ctcarrier_at_gmail.com> wrote:
>> 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
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>