【问题标题】:Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json线程“主”javax.xml.bind.PropertyException 中的异常:名称:eclipselink.media-type 值:application/json
【发布时间】:2014-01-24 13:41:18
【问题描述】:

我正在尝试遵循位于 here 的示例,但得到一个 javax.xml.bind.PropertyException。由于以下代码行,我收到此异常:

marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");

我已经复制/粘贴了上面列出的示例,所以我的代码正是您在此处看到的。搜索 SO 和 Google 并没有帮助,我想我会把它带给 SO 的天才寻求帮助。任何帮助将不胜感激,使用 json.org、Jackson 和 JAXB 使用 JSON 和 XML 进行(反)序列化已经变成了一个黑色无底坑,已经消耗了我将近一个月的时间。

我的第一印象是我没有正确指定 eclipselink 运行时 (as described here),但这并没有产生解决方案。

堆栈跟踪:

Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
   at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:358)
   at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
   at HelloWorld.main(HelloWorld.java:17)

这就是我正在做的,

【问题讨论】:

  • 您使用的是 EclipseLink 2.4.0 或更高版本吗?
  • @sasankad - 很好的收获。抛出的异常是 RI 异常(com.sun.xml.internal.bind.v2.runtime 包),因此 EclipseLink MOXy 没有被用作 JAXB 提供程序。
  • @BlaiseDoughan,很多人都遇到过这个问题

标签: java json jaxb eclipselink moxy


【解决方案1】:

如果您不想添加 jaxb.properties 文件,您可以在 Java 代码中完成这一切。这对于您不想冒险通过引入新的 jaxb.properties 文件来影响类路径的遗留系统特别有用。

import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.xmlmodel.ObjectFactory;

//Set the various properties you want
Map<String, Object> properties = new HashMap<>();
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);

//Create a Context using the properties
JAXBContext jaxbContext = 
    JAXBContextFactory.createContext(new Class[]  {
       MyClass.class,    ObjectFactory.class}, properties);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

//Marshall the object
StringWriter stringWriter = new StringWriter();
jaxbMarshaller.marshal(myObject, stringWriter);
String json = stringWriter.toString();

【讨论】:

    【解决方案2】:

    在我添加的主要方法中(你也可以使用-D):

    System.setProperty("javax.xml.bind.context.factory","org.eclipse.persistence.jaxb.JAXBContextFactory");
    

    【讨论】:

    • 欢迎来到 SO。请在您的答案中格式化代码,并考虑解释您的答案为何有效。另外,请浏览how to answer guide
    【解决方案3】:

    您的类路径中需要有 EclipseLink jar(2.4.0 或更高版本),并且在用于引导 JAXBContext 的类所在的包中包含一个 jaxb.properties 文件,其中包含以下条目:

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

    以下是 GitHub 上一个示例的链接,您可以运行该示例以查看一切正常:

    【讨论】:

    • 感谢您的回答!你看到我的 PrintScr 了吗?我已经下载了最后一个 EclipseLink 并创建了名为 jaxb.properties 的文件。有什么问题?
    • 请详细说明“jaxb 的问题”。
    • 为了让它变得非常明显:jaxb.properties 文件必须在 resources 树 中的同一个包中,而不是在 src 树 中。请参阅 Blaise 的示例代码。
    • 澄清 jaxb.properties 必须在 model 类的包中。因此,在您的示例中,Customer 类所在的类中。我试图将属性文件放入与HelloWorld 对应的类所在的包中。
    猜你喜欢
    • 2017-07-20
    • 2013-05-27
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多