3

Every book on REST uses <atom:link href="..." rel="..."> to define Hypermedia in RESTful apps; but Jersey (with the use of JAXB) do not seems to have this support.

I've tried @XmlSchema in package-info.java as explained here; I've also tried extendingNamespacePrefixMapper as explained there. But none works and output this at best:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer xmlns:ns2="http://www.w3.org/2005/Atom">
    <first-name>Roland</first-name>
    <ns2:link href="/customer/1" rel="self" />
</customer>

Using namespace and, as a result, Atom, seems impossible in Jersey. I've miss something?

ps. I'me using a XSD to generate @XmlElement classes, and, for the moment, I create my own Link class. Is there a schema or a JAR to do that (jersey-atom maven dependency uses rome but without any help)

Community
  • 1
  • 1
yves amsellem
  • 7,106
  • 5
  • 44
  • 68
  • is your issue that you cannot get the links in at all, or that you want the links to have the "atom" namespace prefix? – jayraynet May 22 '11 at 19:05
  • I want the namespace prefix. I want also to know if I have to create the AtomLink class myself. Thanks. – yves amsellem May 22 '11 at 20:45
  • 2
    RESTEasy has its own solution to this: http://docs.jboss.org/resteasy/2.0.0.GA/userguide/html_single/#LinkHeader (similar to jayraynet's idea but using annotations to make it easier) and I think I read somewhere that it is likely to be part of the JAX-RS 2.0 spec (which presumably means it will be part of Jersey as well). – Tyler May 23 '11 at 17:07

2 Answers2

3

(Assuming that you are not concerned with the namespace prefix and just want to create the links)

Here is my approach to creating the links. In my resource class (the jersey rest service), I return a java object (below "Person"), whose class is decorated with jaxb annotations. One of the properties returns atom link objects.

@XmlRootElement(namespace = Namespace.MyNamespace)
public class Person implements Serializable {
    private AtomLinks links = null;

    @XmlElement(name = "link", namespace = Namespace.Atom)
    public AtomLinks getLink() {
        if (this.links == null) {
            this.links = new AtomLinks();
        }

        return this.links;
    }
..
}

@XmlAccessorType(value = XmlAccessType.NONE)
public class AtomLinks extends ArrayList<AtomLink> {
..
}

@XmlAccessorType(value = XmlAccessType.NONE)
public class AtomLink implements Serializable {
    @XmlAttribute(name = "href")
    public URI getHref() {
        return href;
    }
    @XmlAttribute(name = "rel")
    public String getRel() {
        return rel;
    }
    @XmlAttribute(name = "type")
    public String getType() {
        return type;
    }
    @XmlAttribute(name = "hreflang")
    public String getHreflang() {
        return hreflang;
    }
    @XmlAttribute(name = "title")
    public String getTitle() {
        return title;
    }
..
}

public class Namespace {
    public final static String Atom = "http://www.w3.org/2005/Atom";
..
}

Prior to returning my object ("Person") I fill in the links, creating a self link and links to other related links. I utilize the uriInfo object that jersey injects to get the base url. If this is helpful but you would like more of the example, let me know and I will fill in the gaps.

jayraynet
  • 896
  • 1
  • 7
  • 24
1

If I am right there is an approach in Jersey to inject the links to the objects.

See: Jersey 2.9 User Guide Chapter 6.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Robin Wieruch
  • 14,900
  • 10
  • 82
  • 107