我可以用代码替换 jaxb.properties 吗?

2023-11-25

我正在使用一些非标准扩展来自 EclipseLink 的 JAXB 实现,为了启用该实现,我必须使用 jaxb.properties 来配置它。效果很好。

然而,由于构建错误,属性文件没有包含在正确的位置,导致使用默认的 JAXB,它没有任何错误,只是继续解析 XML 文件,忽略非标准扩展名,给我留下了一个非工作豆。

为了使其更加健壮,我想摆脱属性文件并在代码中指定上下文配置。由于 EclipseLink 的注释,我已经在编译时依赖了它们,并且我不需要在部署时配置这部分(事实上,看到可能出错的地方,我不希望它可配置)。


您可以执行以下操作来获取 EclipseLink JAXB (MOXy)JAXBContext没有jaxb.properties file:

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        //JAXBContext jc = JAXBContext.newInstance(Animals.class);
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {Animals.class}, null);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum6871469/input.xml");
        Animals animals = (Animals) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(animals, System.out);
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以用代码替换 jaxb.properties 吗? 的相关文章

随机推荐