users@jersey.java.net

Re: [Jersey] Interlinking resources

From: James Strachan <james.strachan_at_gmail.com>
Date: Wed, 25 Mar 2009 15:22:22 +0000

2009/3/25 Imran M Yousuf <imyousuf_at_smartitengineering.com>:
> Hi,
>
> I have a generic REST question and am wondering how to achieve it in
> using JAX-RS and Jersey.
>
> I would take an example to illustrate the scenario. There is a obejct
> 'A' which is a resource and there is 'B' which is also a resource  Now
> 'A' has one or more 'B'. Now when resource 'A' is accessed it will
> 'have' 'B' as well, in terms of REST we would want them to referred to
> using their respective URIs. Now my question is if I am using
> JAXB/JSON for exporting my resources how can I achieve this?

FWIW I've hit similar issues; needing to refer to related resources
from other resources. I've tended to add URI attributes to the JAXB
beans so at least there's some way to return the links in the XML/JSON
representation.

Its not very DRY as the URI path is then encoded once in @Path and
then in one or more JAXB beans via their URI attribute - for each bean
type which refers to a B. There's been a few threads about this in the
past on this list. Ideally one day we'd be able to just store (say)
the ID of the related resource and from the related @Path annotation
some utility could automatically generate the URI for a resource bean.


e.g. it'd be nice to do something like this one day...

public class A {
 private String idForB;

  public String getUriForB() {
     return UriBuilder.for(B.class).withId(idForB);
 }
}

@Path("/b/{id}")
public class B {
  @XmlID
  private String id;

 ...
}


Then something like this would work

  A a = new A();
  a.setIdForB("abc");

  String uri = a.getUriForB();
  assertEquals("/b/abc", uri);

i.e. we have a single @Path annotation on the owner (B) which can then
be referred to by anyone who links to it.

It would be great if we could try reuse the same path expression from
the @Path on the resource bean (as opposed to the DTO object) to make
it even more DRY - but given the navigation paths between resources,
thats maybe tricky.

-- 
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/