简体   繁体   English

JAXB 或 Jackson 用于 xml 解组?

[英]JAXB or Jackson for xml unmarshalling?

Receiving an XML response, there are two main solutions decoding xml to a DTO object.接收XML响应时,有两种主要的解决方案将 xml 解码为 DTO 对象。 Why should one prefer JAXB over jackson , or the other way around?为什么应该更喜欢JAXB不是jackson ,或者相反?

JAXB: JAXB:

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlString));
unmarshaller.unmarshal(reader, DtoObject.class);

Jackson:杰克逊:

mapper = new ObjectMapper() / new XmlMapper();
mapper.readValue(xmlString, DtoObjectc.class);

In my opinion, I will use JAXB, the following reason.在我看来,我会使用JAXB,原因如下。

  1. It is quite matured and part of JDK.它已经相当成熟并且是 JDK 的一部分。
  2. I do not need third party additional library to use我不需要第三方附加库来使用
  3. Jackson2 XML transformation is new as compared to JAXB which is in the industry for a quite long time and there are lot of community for it.与 JAXB 相比,Jackson2 XML 转换是新的,JAXB 在行业中已经存在了很长时间,并且有很多社区。

However I am not saying that which is good or bad.然而,我并不是说这是好是坏。 Again it is a choice to the developer to use.同样,它是开发人员使用的选择。

From functionality, there is no difference.从功能上来说,没有区别。 All is for java object <--> xml object.全部用于 java 对象 <--> xml 对象。 However, there is performance difference.但是,存在性能差异。 I have tested with Jmeter between JAXB and Dozer Mapper.我已经在 J​​AXB 和 Dozer Mapper 之间使用 Jmeter 进行了测试。 Result shows JAXB is about 50% efficient.结果显示 JAXB 的效率约为 50%。 I don't know what about Jackson.我不知道杰克逊怎么样。 But from my personal experience.但是从我个人的经验来看。 I prefer to JAXB.我更喜欢JAXB。 For your reference.供你参考。

XmlMapper requires less configuration and on edge cases like PascalCase mapping it works when jaxb2 does not. XmlMapper 需要较少的配置,并且在像PascalCase映射这样的边缘情况下,它可以在 jaxb2 不工作时工作。

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.9.8</version>
</dependency>

Use the same version as the other things you have with com.fasterxml.使用与 com.fasterxml 中的其他内容相同的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM