【发布时间】:2018-10-25 14:38:06
【问题描述】:
我能够使用 Oracle JDK 1.8 标准库将以下结构解组为 List。
<parent>
<child>1234 1234 1234</child>
<child>1231 1313 1331</child>
</parent>
我有一个像下面这样的 XmlAdapter 类来将 String 标记为 int[],反之亦然,并在下面的根 XML 类上使用 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,唯一的方法是我需要创建一个 Wrapper 类来保存下面的 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>。
令人惊讶的是,List<int[]> children 现在在结果对象中变成了List<ChildWrapper> children。我没有改变父母,所以解组器现在通过反射或其他东西创建一个不同的对象?这不是坏了吗?
【问题讨论】:
-
破碎是什么意思?你得到不同的结果吗?得到错误?什么?
-
我遇到了一个异常,等一下我尝试重新生成它。
-
调试它,似乎解组器试图将 int[] 的结果直接分配给孩子。意外地我找到了一种让它工作的方法,但它似乎对我来说不合适......
标签: java jaxb eclipselink moxy