使用 JAXB 将列表包装器转换为列表

发布于 2024-10-06 09:52:38 字数 1207 浏览 0 评论 0原文

我正在尝试使用 JAXB 绑定注释来注释一组数据对象,以便可以使用 CXF 将这组数据对象正确封送为 JSON。我遇到了一个类的问题,该类基本上只是 ArrayList 的包装器:

class IntegerListWrapper {
    private ArrayList<Integer> integerList;
    ...
}

我的一些数据对象引用了这个类:

class DataObjectFoo {
    ...
    public IntegerListWrapper getDataIDs() {
        ...
    }
    ...
}

我正在寻找输出:

"DataObjectFoo" : {
  "dataIDs" : [1, 2, ..., n] // Array of Data IDs
}

我尝试注释 IDList 类本身,但它离开了我与此:

"DataObjectFoo" : {
  "dataIDs" : { "integerList" : [1, 2, ..., n] } // Extra nesting
}

我尝试编写一个 XmlAdapter 但得到了混合的结果:

// Throws an error... "Can't bind to interface"
public final class IDListAdapter extends XmlAdapter<List<Integer>, IDList> {
// Does not produce any output
public final class IDListAdapter extends XmlAdapter<ArrayList<Integer>, IDList> {
// Produces output with extra nesting like above
public final class IDListAdapter extends XmlAdapter<Integer[]>, IDList>

所以我有两个问题:

  1. 如何获得所需的输出(无需将 IDList 转换为数据对象中的其他内容)?
  2. 为什么第二个 XmlAdapter(使用 ArrayList)没有产生输出?

I'm trying to annotate a set of data objects with JAXB binding annotations so that this set of data objects can be properly marshaled as JSON using CXF. I'm running into an issue with a class that is basically just a wrapper around ArrayList:

class IntegerListWrapper {
    private ArrayList<Integer> integerList;
    ...
}

Some of my data objects refer to this class:

class DataObjectFoo {
    ...
    public IntegerListWrapper getDataIDs() {
        ...
    }
    ...
}

I'm looking for the output to be:

"DataObjectFoo" : {
  "dataIDs" : [1, 2, ..., n] // Array of Data IDs
}

I tried annotating the IDList class itself but it left me with this:

"DataObjectFoo" : {
  "dataIDs" : { "integerList" : [1, 2, ..., n] } // Extra nesting
}

I tried writing an XmlAdapter but got mixed results:

// Throws an error... "Can't bind to interface"
public final class IDListAdapter extends XmlAdapter<List<Integer>, IDList> {
// Does not produce any output
public final class IDListAdapter extends XmlAdapter<ArrayList<Integer>, IDList> {
// Produces output with extra nesting like above
public final class IDListAdapter extends XmlAdapter<Integer[]>, IDList>

So I have two questions:

  1. How do I get the desired output (without converting IDList to something else in my data objects)?
  2. Why did the second XmlAdapter (using ArrayList) not produce output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-10-13 09:52:38

我不确定你可以使用它周围的额外包装类。你不能只让“getDataIds()”调用返回列表吗?

I'm not sure you can with the extra wrapper class around it. Can you not just make the "getDataIds()" call return List?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文