我正在开发一个使用 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)
Run Code Online (Sandbox Code Playgroud)
我测试的代码是:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我在 pom.xml 中添加了 EclipseLink 依赖项:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
jaxb.properties并且,我添加了包含内容的文件: …