Moxy 无法解组覆盖超类的子类字段

Moxy can't unmarshal subclass field which overide supperclass

当使用Moxy 解组样本xml 到child 时,它总是无法获取名称。它始终为空。

样本xml

<?xml version="1.0" encoding="UTF-8"?>
<child>
    <name value="test"/>
</child>

样本class

public class Parent {

    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

@XmlRootElement
public class Child extends Parent {

    @Override
    @XmlPath("name/@value")
    public String getName() {
        return super.getName() == null ? "" : super.getName();
    }

    @Override
    public void setName(String name) {
        super.setName(name);
    }
}


JAXBContext jc2 = JAXBContext.newInstance(Child.class);
Unmarshaller unmarshaller = jc2.createUnmarshaller();
Child child = (Child) unmarshaller.unmarshal(new File("d:\sample.xml"));

如果我无法对 Parent class.

进行任何更改,我如何获得此值

谢谢,

http://blog.bdoughan.com 和 Whosebug 挖掘后。

好的,我终于在 Whosebug 上找到了这些

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    version="2.6.0">
    <java-types>
        <java-type name="com.abc.Parent" xml-transient="true" />
    </java-types>
</xml-bindings>

有代码

    Map<String, Source> metadata = new HashMap<String,Source>();
    metadata.put("com.abc", new StreamSource( Volume.class.getClassLoader().getResourceAsStream("sample.xml"))); 
    Map<String,Object> properties = new HashMap<String,Object>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadata);
    JAXBContext jc2 = JAXBContext.newInstance(new Class[] {Child.class}, properties);

然后可以 get/set 超类中的值。

如果您使用maven,本文可能会帮助您确定xmlbinding 文件的位置。

How do I solve EclipseLink's (MOXy) 'getting property "eclipselink.oxm.metadata-source" is not supported'?