如何在EclipseLink中创建JAXBContext?

发布于 2024-10-21 14:41:09 字数 999 浏览 5 评论 0原文

JAXBContext.newInstance() 方法接收 Map 并将属性作为参数。

我应该指定哪些属性以及如何使 JAXBContext 实例了解多个具有 xml 映射的文件?

示例展示了如何以某种方式仅指定一个文件这在实际系统中是不可接受的(即使用new File(address))。相反,我需要使用返回 URL 对象的 classLoader.getResource()

谢谢!

UPD:

当我尝试实例化 JAXBContext 时,我收到此异常:

javax.xml.bind.JAXBException: property "eclipselink-oxm-xml" is not supported
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:118)
    ...

很明显,启动时找不到 jaxb.properties 文件。我应该把它放在哪里? (我正在使用maven,实际上我尝试将其放入/target/jaxb.properties/target/classes/jaxb.properties/target/classes/META-INF/definitions/jaxb.properties - 它靠近 xml 映射文件,而 /target/classes/com/myproject/experiment/jaxb.properties - 它是靠近尝试实例化 JAXBContext 的类)。

JAXBContext.newInstance() method receives Map with properties as a parameter.

Which properties and how should I specify to make JAXBContext instance informed about several files with xml-mappings?

Example shows how to specify only one file in a way which is not acceptable in real systems (i.e. with new File(address)). Instead of this I need to use classLoader.getResource() which returns URL object.

Thanks!

UPD:

When I try to instantiate JAXBContext I get this exeption:

javax.xml.bind.JAXBException: property "eclipselink-oxm-xml" is not supported
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:118)
    ...

It's obvious that jaxb.properties file is not found at start up. Where should I put it? (I'm using maven, and actually I've tried to put it to /target/jaxb.properties, /target/classes/jaxb.properties, /target/classes/META-INF/definitions/jaxb.properties - it's near xml-mapping file, and /target/classes/com/myproject/experiment/jaxb.properties - it's near the class which tries to instantiate JAXBContext).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-10-28 14:41:09

EclipseLink JAXB (MOXy) 外部绑定文件可以位于任何(或列表中) of) 以下格式:

  • java.io.File
  • java.io.InputStream
  • java.io.Reader
  • java.net.URL
  • javax.xml.stream.XMLEventReader
  • javax.xml.stream.XMLStreamReader
  • javax.xml.transform.Source
  • org.w3c .dom.Node
  • org.xml.sax.InputSource

要使用 MOXy 作为 JAXB 提供程序,您还需要在模型类中包含一个名为 jaxb.properties 的文件,其中包含以下条目:

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

The EclipseLink JAXB (MOXy) external bindings file can be in any of (or a List of) the following formats:

  • java.io.File
  • java.io.InputStream
  • java.io.Reader
  • java.net.URL
  • javax.xml.stream.XMLEventReader
  • javax.xml.stream.XMLStreamReader
  • javax.xml.transform.Source
  • org.w3c.dom.Node
  • org.xml.sax.InputSource

To use MOXy as your JAXB provider you also need to include a file named jaxb.properties with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文