加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

MOXy JAXB javax.xml.bind.PropertyException

发布时间:2020-12-14 06:04:12 所属栏目:Java 来源:网络整理
导读:我按照这个例子: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JSON_Twitter 现在我有这个课: import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBElement;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import
我按照这个例子: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JSON_Twitter

现在我有这个课:

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);
    }
}

我有jaxb.properties:

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

如果我运行此代码,我得到:

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)

我怎样才能解决这个问题?

我搜索了SO和Google,这些答案都不起作用:

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

解决方法

您需要确保您的jaxb.properties文件与用于引导JAXBContext的域类位于同一个包中,并且EclipseLink MOXy位于类路径上.

> http://blog.bdoughan.com/search/label/jaxb.properties

如果您使用的是Maven,则jaxb.properties文件应位于以下位置,假设Foo位于名为com.example.Foo的包中:

> src / main / resources / com / example / foo / jaxb.properties
> src / main / java / com / example / foo / Foo.class

有关完整示例,请参阅:

> https://github.com/bdoughan/blog20110819

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读