我可以用代码替换jaxb.properties吗? [英] Can I replace jaxb.properties with code?

查看:104
本文介绍了我可以用代码替换jaxb.properties吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些非EclipseLink的JAXB实现的标准扩展,为了实现该实现,我必须使用jaxb.properties进行配置。运行良好。

I am using some non-standard extensions from EclipseLink's implementation of JAXB, and to enable that implementation, I have to configure it using jaxb.properties. Works well.

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

However, due to a build error, the properties file was not included in the proper place, resulting in the default JAXB being used, which without any errors just continued to parse the XML file, ignoring the non-standard extension, leaving me with a non-working bean.

为了使这个更强大,我想得到删除属性文件并在代码中指定上下文配置。我已经有了EclipseLink上的编译时依赖,因为它们有注释,我不需要在部署时配置这个部分(实际上,看看会出现什么问题,我不希望它可配置)。

To make this more robust, I'd like to get rid off the properties file and specify the context configuration in code. I already have a compile-time dependency on EclipseLink because of their annotations, and I do not need this part configurable at deploy time (in fact, seeing what can go wrong, I do not want it configurable).

推荐答案

您可以执行以下操作来获取不带<$的EclipseLink JAXB(MOXy) JAXBContext c $ c> jaxb.properties file:

You could do the following to get an EclipseLink JAXB (MOXy) JAXBContext without a 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);
    }

}

这篇关于我可以用代码替换jaxb.properties吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆