Java 使用 eclipselink.media-type 值设置 Marshaller 属性时出现 PropertyException:application/json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18498635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:36:35  来源:igfitidea点击:

PropertyException when setting Marshaller property with eclipselink.media-type value: application/json

javajaxbeclipselinkmarshallingmoxy

提问by Prmths

I'm attempting to follow the example located herebut get an javax.xml.bind.PropertyException. I receive this exception because of the following line of code:

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

marshaller.setProperty("eclipselink.media-type", "application/json");

I have literally copy/pasted the example listed above so my code is exactly what you see there. Searching SO and Google for this has not been helpful, and thought I'd bring this to the geniuses at SO for some help. Any help would be most appreciated, (de)serialization with JSON and XML with json.org, Hymanson, and JAXB has turned into a black and bottomless pit that has consumed almost a month of my life.

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

My first impression was that I wasn't properly specifying the eclipselink runtime (as described here)but that didn't produce a solution.

我的第一印象是我没有正确指定 eclipselink 运行时(如此处所述),但这并没有产生解决方案。

Stacktrace:

堆栈跟踪:

Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json   
  at org.eclipse.persistence.jaxb.JAXBMarshaller.setProperty(JAXBMarshaller.java:528)
  at com.dualoutput.DualOutput.main(DualOutput.java:20)

SSCCE

南昌

采纳答案by bdoughan

You need be sure you are using EclipseLink 2.4.0 or above. The current version is 2.5.0 which can be downloaded at (or obtained from Maven Central):

您需要确保您使用的是 EclipseLink 2.4.0 或更高版本。当前版本为 2.5.0,可以在(或从 Maven Central 获得)下载:



UPDATE

更新

MOXy also offers the following convenience classes to access the extension properties:

MOXy 还提供了以下便利类来访问扩展属性:

  • org.eclipse.persistence.jaxb.JAXBContextProperties
  • org.eclipse.persistence.jaxb.MarshllerProperties
  • org.eclipse.persistence.jaxb.UnmarshallerProperties
  • org.eclipse.persistence.jaxb.JAXBContextProperties
  • org.eclipse.persistence.jaxb.MarshllerProperties
  • org.eclipse.persistence.jaxb.UnmarshallerProperties

This means you could do the following:

这意味着您可以执行以下操作:

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