fab*_*bby 5 java spring json jaxb
我正在开发一个使用 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并且,我添加了包含内容的文件:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我仍然收到此错误,因为当我在这个论坛上阅读时,我认为我已经完成了所有操作。我想提的是,我测试的代码位于目录中:
src\main\java\com\fabbydesign\controller
Run Code Online (Sandbox Code Playgroud)
该文件jaxb.properties位于目录中:
src\main\resources\com\fabbydesign\controller
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
小智 5
简单,您可以指定系统变量,例如:
System.setProperty("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
Run Code Online (Sandbox Code Playgroud)
这对我有用。
我不确定 jaxb.properties 文件的位置是否正确。堆栈跟踪仍然包含 com.sun.xml.internal.bind.v2.runtime 类,这表明仍在使用错误的工厂类。
在这里查看更多信息或设置工厂的替代方法:Where to include jaxb.properties file?
| 归档时间: |
|
| 查看次数: |
6031 次 |
| 最近记录: |