gpt4 book ai didi

javax.xml.bind.PropertyException : name: eclipselink. 媒体类型值:application/json

转载 作者:行者123 更新时间:2023-12-01 16:59:47 24 4
gpt4 key购买 nike

我正在开发一个使用 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

我做错了什么?

最佳答案

简单,您可以指定系统变量,例如:

System.setProperty("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory");

这对我有用。

关于javax.xml.bind.PropertyException : name: eclipselink. 媒体类型值:application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42531483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com