Java JAXB 映射到 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19158056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB Mapping to JSON
提问by Artem Moskalev
I have written a JAX-RS (Jersey) REST Service, which accepts XML messages of ONIX XML format. Generally, I have generated all the required classes for JAXB binding from the given schema with xjc. There are more than 500 classes overall and I cannot modify them.
我编写了一个 JAX-RS (Jersey) REST 服务,它接受 ONIX XML 格式的 XML 消息。通常,我已经使用 xjc 从给定的模式生成了 JAXB 绑定所需的所有类。总共有 500 多个类,我无法修改它们。
Now, when I have a JAXB-mapped object, I need to store it to the database. I work with mongoDb, so the message format should be JSON. I tried to use Hymanson with JAXB module to convert JAXB-object into JSON, which works pretty fine with storing the data. But when I try to convert the JSON back into the JAXB object, it throws an exception connected somehow with the JAXBElement. In google I found out that the JAXBElement is not supported in Hymanson and I have to work around this issue. But I cant do it because I cannot modify JAXB-generated classes.
现在,当我有一个 JAXB 映射对象时,我需要将它存储到数据库中。我使用 mongoDb,所以消息格式应该是 JSON。我尝试使用带有 JAXB 模块的 Hymanson 将 JAXB 对象转换为 JSON,这对于存储数据非常有效。但是当我尝试将 JSON 转换回 JAXB 对象时,它会抛出一个与 JAXBElement 以某种方式连接的异常。在 google 中,我发现 Hymanson 不支持 JAXBElement,我必须解决这个问题。但我不能这样做,因为我无法修改 JAXB 生成的类。
Is there a way to map JAXB Objects into JSON with some other means, but which will follow the whole JAXB specification so that I have no problems in the future converting from JSON to the JAXB object and visa vera?
有没有办法用其他方法将 JAXB 对象映射到 JSON,但是这将遵循整个 JAXB 规范,以便我将来从 JSON 转换为 JAXB 对象和签证没有问题?
采纳答案by bdoughan
Note:I'm the EclipseLink JAXB (MOXy)lead and a member of the JAXB (JSR-222)expert group.
注意:我是EclipseLink JAXB (MOXy) 的负责人和JAXB (JSR-222)专家组的成员。
If you can't do it with Hymanson, you use case will work with MOXy.
如果您不能使用 Hymanson,您的用例将与 MOXy 一起使用。
Java Model
Java模型
Foo
富
Here is a sample class that contains a field of type JAXBElement
.
这是一个包含类型字段的示例类JAXBElement
。
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlElementRef(name="bar")
private JAXBElement<Bar> bar;
}
Bar
酒吧
public class Bar {
}
ObjectFactory
对象工厂
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(name = "bar")
public JAXBElement<Bar> createBar(Bar bar) {
return new JAXBElement<Bar>(new QName("bar"), Bar.class, bar);
}
}
Standalone Demo Code
独立演示代码
Below is some demo code you can run in Java SE to see that everything works:
下面是一些演示代码,您可以在 Java SE 中运行以查看一切正常:
Demo
演示
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class, ObjectFactory.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource json = new StreamSource("src/forum19158056/input.json");
Foo foo = unmarshaller.unmarshal(json, Foo.class).getValue();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
input.json/Output
输入.json/输出
{"bar" : {} }
Running with JAX-RS
使用 JAX-RS 运行
The following links will help you leverage MOXy in a JAX-RS service:
以下链接将帮助您在 JAX-RS 服务中利用 MOXy:
回答by Archimedes Trajano
Since you're using Hymanson you can construct an ObjectMapper
with the JaxbAnnotationModule
and write out the value. The following is some code to write a JAXB annotated object to system out.
由于您使用的Hyman逊,你可以构造一个ObjectMapper
与JaxbAnnotationModule
和写出来的值。以下是将 JAXB 注释对象写入系统输出的一些代码。
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());
mapper.writeValue(System.out, jaxbObject);
This approach will also work on Glassfish as it does not utilize the provider given by the container which would cause ClassNotFoundException as per GLASSFISH-21141
这种方法也适用于 Glassfish,因为它不使用容器提供的提供程序,这会导致 ClassNotFoundException 根据GLASSFISH-21141