线程“main”中的异常 javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20962053/
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-13 05:35:41  来源:igfitidea点击:

Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json

javajsonjaxbeclipselinkmoxy

提问by user3167333

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(MarshallerProperties.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 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)

This is what I'm doing,

这就是我正在做的

enter image description here

enter image description here

采纳答案by bdoughan

You need to have the EclipseLink jars (2.4.0 or newer) on your class path, and a jaxb.propertiesfile in the same package as the classes used to bootstrap theJAXBContextwith the following entry:

你需要在你的类路径上有 EclipseLink jars(2.4.0 或更新),以及一个jaxb.properties与用于引导的类在同一个包中的文件,JAXBContext包含以下条目:

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

Below is a link to an example on GitHub that you can run to see everything working:

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

回答by weller1

To my main method I added (you could also use -D):

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

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

回答by Jon

If you don't want to add a jaxb.properties file, you can do this all in Java code. This is especially helpful for legacy systems where you don't want to risk affecting the classpath by introducing a new jaxb.properties file.

如果您不想添加 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();