users@jersey.java.net

Re: [Jersey] Connectedness, Jaxb and link paths

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 24 Sep 2008 09:34:59 -0400

On Sep 24, 2008, at 7:10 AM, tarjei wrote:
>
> I got two usecases:
>
> 1. A field with a link to a testservice for that url:
>
> <url href="/myservice/test/http://test.com">http://test.com</url>
>
> 2. Representing a mapping (*:* or 1:*) in the representation without
> showing the complete object:
> <subscribers>
> <subscriber href="/subscribers/jonas_at_sm.com" id="1" email="jonas_at_sm.com
> "/>
> <subscriber href="/subscribers/jonas_at_sm.com" id="1" email="jonas_at_sm.com
> "/>
> </subscribers>
>
> I have already implemented an adapter to that works fairly well,
> however
> it is not general, so if anyone has some tips on how I can create a
> general linkadapter that will work without me defining the link every
> time, that would be great. I'm planning to build a rest interface
> against a DB with 50+ tables soon and I'll be needing it then.
>
> With regard to the first one, I want to keep the links inside the
> resource somehow but with jax not knowing about them. Should I just
> use
> some separate namespace for the links, like xlink?
>
I don't know how you'd define a general type adaptor, I think you'd
either have to use the @XmlJavaTypeAdapter on each field that you want
to make into a link or put it on the class assuming you always want
links to instances of those types.

> Also, the last part of this question is, is there a general way to
> generate absolute links?
>
> For example, if the service is running bellow the url /services/ on a
> host named myservice running on port 8080, I want the links to be to
> http://myservice:8080/services/...
>
Use

@Context UriInfo u;

to have Jersey inject the UriInfo into a field of your resource class.
Then get a base URI builder from the instance when you want to build
the absolute URI.

URI link = u.getBaseUriBuilder().path(somePath).build();

Note that you can pass a resource class to the path method and the
UriBuilder will extract the @Path template to save you having to
duplicate string constants.

Marc.

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