1

is it possible to make use of the concept of shared references with JSON as output mode? I read this article http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html), but changing the @Produces on my JAX-RS to JSON forces an endless loop. Basically i want to reduce an object to it`s id:

public class Foo {
    private long id;
    private String someText;
    private Bar bar;
}

I want this to bind instances of this like so:

{
    "id": 1234,
    "someText": "lorem",
    "bar_id": 9876
}

This is what i want to avoid:

{
    "id": 1234,
    "someText": "lorem",
    "bar": {
        "id": 9876,
        "anotherText": "ipsum"
    }
}
4

1 回答 1

1

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

使用 MOXy 作为您的 JSON 绑定提供程序,您引用的共享引用帖子(来自我的博客)将适用于 JSON,就像它适用于 XML 一样。由于您使用的是 JAX-RS,因此下面是在该环境中配置 MOXy 的示例:

使用@XmlID/@XmlIDREF时,预计通过其 ID 引用的对象也存在于文档中。由于这不是您的用例,您最好使用XmlAdapter. 的XmlAdapter签名类似于:

public class BarAdapter extends XmlAdapter<Integer, Bar> {
    ...
}

在编组期间,XmlAdapter您将负责从实例返回 id ,并负责在解组期间返回基于Bar的实例。Barid

于 2013-05-21T13:29:46.860 回答