[英]JAXB Mapping to JSON
I have written a JAX-RS (Jersey) REST Service, which accepts XML messages of ONIX XML format. 我编写了一个JAX-RS(Jersey)REST服务,它接受ONIX XML格式的XML消息。 Generally, I have generated all the required classes for JAXB binding from the given schema with xjc.
通常,我已经使用xjc从给定模式生成了JAXB绑定的所有必需类。 There are more than 500 classes overall and I cannot modify them.
整体上有500多个课程,我无法修改它们。
Now, when I have a JAXB-mapped object, I need to store it to the database. 现在,当我有一个JAXB映射对象时,我需要将它存储到数据库中。 I work with mongoDb, so the message format should be JSON.
我使用mongoDb,因此消息格式应该是JSON。 I tried to use Jackson with JAXB module to convert JAXB-object into JSON, which works pretty fine with storing the data.
我尝试使用Jackson和JAXB模块将JAXB对象转换为JSON,这对于存储数据非常有用。 But when I try to convert the JSON back into the JAXB object, it throws an exception connected somehow with the JAXBElement.
但是当我尝试将JSON转换回JAXB对象时,它会以某种方式抛出与JAXBElement连接的异常。 In google I found out that the JAXBElement is not supported in Jackson and I have to work around this issue.
在谷歌我发现杰克逊不支持JAXBElement,我必须解决这个问题。 But I cant do it because I cannot modify JAXB-generated classes.
但我不能这样做,因为我无法修改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对象和签名没有问题?
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 Jackson, you use case will work with MOXy. 如果你不能与杰克逊一起做,你使用案例将适用于MOXy。
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 的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);
}
}
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 input.json /输出
{"bar" : {} }
The following links will help you leverage MOXy in a JAX-RS service: 以下链接将帮助您在JAX-RS服务中利用MOXy:
Since you're using Jackson you can construct an ObjectMapper
with the JaxbAnnotationModule
and write out the value. 由于您使用的是Jackson,您可以使用
JaxbAnnotationModule
构造一个ObjectMapper
并写出值。 The following is some code to write a JAXB annotated object to system out. 以下是将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,因为它没有使用容器给出的提供程序,这会导致根据GLASSFISH-21141引起ClassNotFoundException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.