MOXy JAXB javax.xml.bind.PropertyException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22839359/
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
MOXy JAXB javax.xml.bind.PropertyException
提问by Gergely Fehérvári
I followed this example: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JSON_Twitter
我跟着这个例子:http: //wiki.eclipse.org/EclipseLink/Examples/MOXy/JSON_Twitter
Now I have this class:
现在我有这门课:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty("eclipselink.media-type", "application/json");
unmarshaller.setProperty("eclipselink.json.include-root", false);
StreamSource source = new StreamSource("http://test.url/path/to/resource");
JAXBElement<Foo> jaxbElement = unmarshaller.unmarshal(source, Foo.class);
System.out.println(jaxbElement.getValue().getFoo());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty("eclipselink.json.include-root", false);
marshaller.marshal(jaxbElement, System.out);
}
}
And I have jaxb.properties
:
我有jaxb.properties
:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
If I run this code, I get:
如果我运行此代码,我会得到:
Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.setProperty(AbstractUnmarshallerImpl.java:352)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.setProperty(UnmarshallerImpl.java:450)
at com.example.JavaSEClient.main(JavaSEClient.java:19)
How can I fix this?
我怎样才能解决这个问题?
I searched SO and Google, these answers are not working:
我搜索了 SO 和 Google,这些答案不起作用:
PropertyException when setting Marshaller property with eclipselink.media-type value: application/jsonJAXB javax.xml.bind.PropertyException
使用 eclipselink.media-type 值设置 Marshaller 属性时出现 PropertyException:application/json JAXB javax.xml.bind.PropertyException
采纳答案by bdoughan
You need to make sure that your jaxb.properties
file is in the same package as the domain classes you used to bootstrap the JAXBContext
, and that EclipseLink MOXy is on your class path.
您需要确保您的jaxb.properties
文件与用于引导JAXBContext
.
If you are using Maven, then the jaxb.properties
file should be under the following location assuming Foo
is in a package called com.example.Foo
:
如果您使用的是 Maven,则该jaxb.properties
文件应位于以下位置,假设Foo
位于名为 的包中com.example.Foo
:
- src/main/resources/com/example/foo/jaxb.properties
- src/main/java/com/example/foo/Foo.class
- src/main/resources/com/example/foo/jaxb.properties
- src/main/java/com/example/foo/Foo.class
For a full example see:
有关完整示例,请参见: