gpt4 book ai didi

java - MOXy 编码/解码错误 : javax. xml.bind.PropertyException 由于使用了错误的 JAXBContextFactory

转载 作者:行者123 更新时间:2023-11-30 11:15:05 34 4
gpt4 key购买 nike

我一直在尝试使用 Eclipse MOXy 为我的项目编码/解码一些 JSON 和 XML 文件。经过一组测试后,我遇到了 javax.xml.bind.PropertyException,似乎我的 jaxb.properties 文件没有被读取,因此使用的 JAXBContextFactory 是错误的。

我正在使用 Eclipselink (MOXy) 2.5.2。我已将 jaxb.properties 添加到我的类(class)所在的同一文件夹中,内容如下:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

这是我的测试类:

public static void main(String[] args) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(SIRFCatalog.class);
System.out.println(jaxbContext.getClass());
// SIRFCatalog catalog;
// Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

// ** The exception is thrown in the line below:**
// jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
// jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
// FileInputStream fis = new FileInputStream(new File("/home/philviana/a.json"));
// catalog = (SIRFCatalog) jaxbUnmarshaller.unmarshal(fis);

}

当我运行上面的测试时(请注意有用的代码被注释掉了),我得到以下输出:

class com.sun.xml.bind.v2.runtime.JAXBContextImpl

这表明 MOXy 的 JAXB 上下文工厂没有被加载。所以换句话说,我想使用 org.eclipse.persistence.jaxb.JAXBContextFactory 作为我的 JAXBContextFactory(来自 MOXy),但是我得到了 RI 的类 com.sun.xml.bind.v2.runtime.JAXBContextImpl。

jaxb.properties 文件与我的类和 main 方法在同一目录下(我的项目的唯一类)。这些是我的 JAR:

eclipselink.jar
com.sun.tools.xjc_2.2.0.jar
com.sun.xml.bind_2.2.0.v201004141950.jar
javax.activation_1.1.0.v201108011116.jar
javax.mail_1.4.0.v201005080615.jar
javax.xml.bind_2.2.0.v201105210648.jar
javax.xml.stream_1.0.1.v201004272200.jar
org.eclipse.persistence.antlr-2.5.2.jar
org.eclipse.persistence.asm-2.5.2.jar
org.eclipse.persistence.core-2.5.2.jar
org.eclipse.persistence.moxy-2.5.2.jar
javax.persistence.source_2.1.0.v201304241213.jar
javax.persistence_2.1.0.v201304241213.jar
org.eclipse.persistence.jpa.modelgen.source_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpa.modelgen_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars.source_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars_2.5.2.v20140319-9ad6abd.jar
commonj.sdo_2.1.1.v201112051852.jar

有什么想法吗?

最佳答案

您需要确保 jaxb.properties 文件在类路径中结束。如果您从 Eclipse 运行它,jaxb.properties 可能没有从 src 目录复制到类被编译到的目录。

关于java - MOXy 编码/解码错误 : javax. xml.bind.PropertyException 由于使用了错误的 JAXBContextFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25609869/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com