从未知的JAXBContext(XML)为JSON创建Marshaller [英] Create Marshaller for JSON from unknown JAXBContext (XML)
问题描述
我必须使用一个只提供JAXBContext的lib来为XML对象编组和解组XML数据。此外,我从未见过XML:只有JAXB对象传递给我。我现在需要的是将这些对象转换为XML,而不是转换为JSON。
I have to use a lib that only gives me a JAXBContext to marshall and unmarshall XML data to Java objects. Also I don't ever see XML: Only the JAXB objects are passed to me. What I now need is a conversion of those objects not to XML, but to JSON.
有没有办法从给定的JAXBContext创建一个marshaller,它可以用于生成JSON输出?
情况是我不仅仅是转换数据。我还有逻辑,它作用于XML和JSON之间的Java对象(并操纵数据)。这也是双向转型。 JAXBContext是关于XML和Java对象之间转换的信息 - 我的目的是重用这个上下文信息,而不必使用与JAXB不同的第二种技术实现第二次转换。 JAXBContext(及其Java对象)已经拥有有关XML结构的信息; JAXB自动识别该结构是节省时间的原因。
The situation is that I'm not only transforming data. I also have logic that acts on the Java objects between XML and JSON (and manipulates the data). Also it's a two-way transformation. The JAXBContext is the information I have about the transformation between XML and Java objects - My intention was to reuse this context information for not having to implement a second transformation with a second technology different to JAXB. The JAXBContext (and its Java objects) already have the information about the XML structure; The automated recognition of that structure by JAXB is the time-saving reason for using it.
推荐答案
如果您的JAXB类只使用基本注释,你可以看看 JacksonJAXBAnnotations ,允许杰克逊映射器识别JAXN注释。只需要四行代码(在最简单的编组情况下)。
If your JAXB classes just use the basic annotations, you can take a look at JacksonJAXBAnnotations, allows Jackson mapper to recognize JAXN annotations. Four lines of code (in the simplest marshalling case) would be all you would need.
ObjectMapper mapper = new ObjectMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module);
mapper.writeValue(System.out, yourJaxbObject);
您可以在上面看到所有支持的注释的链接。您需要的maven工件是
You can see the link above for all the supported annotations. The maven artifact you'll need is
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.4.0</version>
</dependency>
- 请参阅github以获取
jackson-module-jaxb-annotations
- 注意这个工件有依赖关系在jackson-core
和jackson-databind
。因此,如果您不使用maven,那么您还需要确保下载这些工件 - See the github for
jackson-module-jaxb-annotations
- Note this artifact has dependencies onjackson-core
andjackson-databind
. So if you're not using maven, then you will need to make sure to download these artifacts also
简单例外:
JAXB类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"hello",
"world"
})
@XmlRootElement(name = "root")
public class Root {
@XmlElement(required = true)
protected String hello;
@XmlElement(required = true)
protected String world;
// Getters and Setters
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<hello>JAXB</hello>
<world>Jackson</world>
</root>
测试
public class TestJaxbJackson {
public static void main(String[] args) throws Exception {
JAXBContext context = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
InputStream is = TestJaxbJackson.class.getResourceAsStream("test.xml");
Root root = (Root)unmarshaller.unmarshal(is);
System.out.println(root.getHello() + " " + root.getWorld());
ObjectMapper mapper = new ObjectMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module);
mapper.writeValue(System.out, root);
}
}
结果
{"hello":"JAXB","world":"Jackson"}
更新
另见这篇文章。看起来MOXy也提供这种支持。
Update
Also see this post. It looks like MOXy also offers this support.
这篇关于从未知的JAXBContext(XML)为JSON创建Marshaller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!