to define Hypermedia in RESTful applications; but Jers...">

Hypermedia with Jersey using Atom

Each REST book is used <atom:link href="..." rel="...">to define Hypermedia in RESTful applications; but Jersey (using JAXB) does not seem to support this support.

I tried @XmlSchemain package-info.java as described here ; I also tried to expand NamespacePrefixMapper as explained there . But no one works and does not 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 the namespace and, as a result, Atom seems to be impossible in Jersey. Did I miss something?

ps. I use XSD to generate classes @XmlElement, and for now I am creating my own Link class. Is there a schema or JAR for this ( jersey-atommaven dependency uses rome, but without any help)

+3
source share
2 answers

(Assuming you are not prefixing the namespace and just want to create links)

Here is my approach to link building. In my resource class (jersey service), I return a java object (below "Person") whose class is decorated with jaxb annotations. One of the properties returns atomic 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";
..
}

Before returning my object ("Person"), I fill out the links, create my own link and link to other related links. I am using a uriInfo object that injects jersey to get the base url. If this is useful, but would like more of this example, let me know and I will fill in the blanks.

+3
source

If I'm right, there is an approach in Jersey that introduces object references.

: Jersey 2.9 6.

+1

All Articles