7

我是 JAXB 的新手,我想使用 EclipseLink MOXy 更改默认命名空间前缀。我的 package-info.java 有以下代码行:

@javax.xml.bind.annotation.XmlSchema (
namespace="http://namespace.mysite.com/",
xmlns = { 
    @javax.xml.bind.annotation.XmlNs(prefix="myns", 
               namespaceURI="http://namespace.mysite.com/")
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED
)
package com.core.mymodel;

我的 jaxb.properties 文件有以下行:

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

我已将 eclipselink.jar 添加到 CLASSPATH,package-info.java 和 jaxb.properties 文件与我的模型类在同一个包中,但是,当我运行程序时,我收到以下错误消息:

javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory        not found
- with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)

我已经用 EclipseLink 版本 2.3.0 和 2.0.2 进行了尝试,但仍然收到相同的错误消息。有谁碰巧知道可能是什么问题?

提前谢谢了

4

2 回答 2

4

jaxb.properties根据异常消息,您的文件似乎正确:

javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory        not found
- with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)

要确认它org.eclipse.persistence.jaxb.JAXBContextFactory在您的类路径中,您可以尝试:

JAXBContext jc = JAXBContextFactory.createContext("com.core.mymodel", null);

要进行更基本的确认,您可以运行:

System.out.println(org.eclipse.persistence.Version.getVersion());

了解更多信息

于 2011-07-25T17:40:14.183 回答
0

将其用作系统属性

System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
于 2020-09-16T07:19:17.623 回答