MOXy JAXB 无法解组到列表中<int[]>

MOXy JAXB unable to unmarshall into List<int[]>

我能够使用 Oracle JDK 1.8 标准库将下面的结构解组为 List。

<parent>
    <child>1234 1234 1234</child>
    <child>1231 1313 1331</child>
</parent>

我有一个 XmlAdapter class,如下所示,将 String 标记为 int[],反之亦然,并在根 XML class 上使用 XMlJavaTypeAdapter,如下所示。

class ChildAdapter extends XmlAdapter<String,int[]> {
    ...
}


@XmlRootElement(name="parent")
class Parent {
    ...
    private List<int[]> children;
    ...
    @XmlElement(name="child")
    @XmlJavaTypeAdapter(ChildAdapter.class)
    public void setChildren(List<int[]> children) {
        ...
    }
    ...
}

但是当我切换到使用 EclipseLink MOXy 实现时出现异常。 有人试过这个吗?

Exception [EclipseLink-33] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Trying to invoke [setChildren] on the object with the value [[I@1f9f6368].  The number of actual and formal parameters differs, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: argument type mismatch
Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[childrenList-->child/text()]
Descriptor: XMLDescriptor(mypackage.Parent --> [DatabaseTable(Parent)])
at org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(DescriptorException.java:714)
at org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor.setAttributeValueInObject(MethodAttributeAccessor.java:286)
at org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor.setAttributeValueInObject(MethodAttributeAccessor.java:239)

...

使用 Moxy,唯一的方法是我需要创建一个包装器 class 来保存如下所示的 int[],但这不是我真正想要的。

class ChildWrapper {
    private int[] childs;
    public void setChilds(int[] childs) {
        this.childs = childs
    }
    public int[] getChilds() {
        return childs;
    }
}

将适配器更改为 class ChildAdapter extends XmlAdapter<String, ChildWrapper>

令人惊讶的是,结果 object 中的 List<int[]> children 现在变成了 List<ChildWrapper> children。我没有更改 parent,所以解组器现在通过反射或其他方式创建不同的 object?这不是坏了吗?

我有一个解决方法。 似乎这里的问题 Moxy 不喜欢 List of List List<List<T>>.

我创建了一个 XML 类型 <String,Object> 的适配器,并将对象转换为列表并将其用于 @XMLJavaTypeAdapter(ChildAdapter.class) 注释。

public class ChildAdapter extends XmlAdapter<String, Object> {

    @Override
    public Object unmarshall(String v) {
         List<Integer> result = new ArrayList<>();
         ...Tokenize your String v and add them to result...
         return result;
    }


    @Override
    public String marshall(Object v) {
         List<Integer> l = (List<Integer)v;
         StringBuilder b = new StringBuilder();
         ...Loop l and append b...
         return b.toString();
    }
}