MOXy/JAXB interface annotation

那年仲夏 提交于 2019-12-09 06:26:26

oxm.xml

You could use EclipseLink JAXB (MOXy's) external binding file to make MOXy think that IInterface is the super class of InterfaceImpl instead of Object.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum16878949">
    <java-types>
        <java-type name="InterfaceImpl" super-type="forum16878949.IInterface"/>
    </java-types>
</xml-bindings>

Demo

Below is how you can specify the mapping document when creating the JAXBContext. For the purpose of this demo I added a getId method to InterfaceImpl so I could show the unmarshalling worked.

package forum16878949;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    private static String MY_PATH = "forum16878949";

    public static void main(String[] args) throws Exception {
        /**
         * Unmarshal code
         */
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum16878949/oxm.xml");
        File f = new File("src/forum16878949/Interface.xml");
        JAXBContext context = JAXBContext.newInstance(MY_PATH, IInterface.class.getClassLoader(), properties);
        Unmarshaller u = context.createUnmarshaller();
        InterfaceImpl i = (InterfaceImpl)u.unmarshal(f);
        System.out.println(i.getId());
    }

}

Interface.xml

<?xml version="1.0" encoding="UTF-8"?>
<InterfaceImpl id="123"/>

Output

123

For More Information

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!