使用Atom的泽西超媒体 [英] Hypermedia with Jersey using Atom

查看:110
本文介绍了使用Atom的泽西超媒体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于REST的每本书都使用< atom:link href =... =...> 来定义RESTful应用程序中的超媒体;但泽西岛(使用JAXB )似乎没有这种支持。

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.

我试过 @ package-info.java中的XmlSchema 如此处所述;我也试过扩展 NamespacePrefixMapper 正如那里所解释的那样。但是没有一个可以输出并最多输出:

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>

使用命名空间,因此,在泽西岛使用Atom似乎是不可能的。我错过了什么?

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

ps。我使用XSD生成 @XmlElement 类,目前我创建了自己的Link类。是否有架构或JAR来做( jersey-atom maven依赖使用 rome 但没有任何帮助)

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)

推荐答案

(假设您不关心命名空间前缀,只想创建链接)

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

这是我创建链接的方法。在我的资源类(泽西休息服务)中,我返回一个java对象(在Person下面),其类用jaxb注释修饰。其中一个属性返回原子链接对象。

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";
..
}

返回我的对象​​之前(人)我填写链接,创建自我链接和链接到其他相关链接。我利用jersey注入的uriInfo对象来获取基本URL。如果这有用,但你想要更多的例子,请告诉我,我将填补空白。

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.

这篇关于使用Atom的泽西超媒体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆