【问题标题】:Converting List wrapper to List using JAXB使用 JAXB 将 List 包装器转换为 List
【发布时间】:2011-05-22 13:41:57
【问题描述】:

我正在尝试使用 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)没有产生输出?

【问题讨论】:

    标签: java json jaxb cxf binding


    【解决方案1】:

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

    【讨论】:

    • 整数包装类被许多外部资源使用,所以我希望避免改变它。我尝试使用 XmlAdapter(扩展 XmlAdapter)。这给了我我正在寻找的格式,但它是一个字符串而不是一个整数数组。我仍然希望(稍长一点)有办法做到这一点。
    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2014-05-20
    • 1970-01-01
    • 2011-10-27
    • 2021-09-17
    • 2020-11-27
    相关资源
    最近更新 更多