Hypermedia with Jersey using Atom

百般思念 提交于 2019-12-03 20:06:39

(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.

Robin Wieruch

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!