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

2022-01-19 00:00:00 marshalling java jaxb eclipselink moxy

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

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

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

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

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, Jackson, and JAXB has turned into a black and bottomless pit that has consumed almost a month of my life.

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

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

堆栈跟踪:

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

推荐答案

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

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):

  • http://www.eclipse.org/eclipselink/downloads/

更新

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

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

  • 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");

相关文章