【发布时间】:2017-07-20 18:22:51
【问题描述】:
我在一个项目中工作,使用 Spring Framework 制作,我想使用 JaxB 将对象转换为 json,我收到此错误:
javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(Unknown Source)
at com.fabbydesign.controller.DashboardController.main(DashboardController.java:82)
我测试的代码是:
public static void main(String[] args) throws ParseException{
ReturnBean rb = new ReturnBean();
rb.setStatus(1);
rb.setMessage("Message here!");
JAXBContext jc;
try {
jc = JAXBContext.newInstance(ReturnBean.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.marshal(rb, System.out);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我在 pom.xml 中添加了 EclipseLink 依赖:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.4</version>
</dependency>
而且,我已经添加了文件jaxb.properties 的内容:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
我不知道为什么我仍然收到这个错误,因为,正如我在这个论坛上看到的那样,我认为我已经做了所有的事情。 我要提的是,我测试的代码在目录中:
src\main\java\com\fabbydesign\controller
并且文件jaxb.properties在目录中:
src\main\resources\com\fabbydesign\controller
我做错了什么?
【问题讨论】: