首页 > 解决方案 > Camel unmarshaller 支持多种数据格式

问题描述

是否有一个 Camel 解组器,我可以使用它来将多种数据格式(JSON、XML 等)解组为 XML?

然后,这个“通用”解组器将用作例如:

<route id="myRoute">
  <from uri="file:test/input"/>
  <!-- The input can be in JSON or in XML -->
  <unmarshal ref="universalUnmarshallerToXML"/>
  <!-- The input payload is always in XML -->
    <choice >
      <when>
          <xpath>/order/customer/country = 'US'</xpath>
          <to uri="file:test/output/us"/>
      </when>
      <when>
          <xpath>/order/customer/country = 'UK'</xpath>
          <to uri="file:test/output/uk"/>
      </when>
      <otherwise>
          <to uri="file:test/output/others"/>
      </otherwise>
  </choice>
</route>

这个通用解组器是否存在(希望它确实存在),还是我应该实现自己的?

谢谢!

标签: apache-cameldataformat

解决方案


它不完全是一个通用的 unmarshaller,但类似于您所要求的:

当你创建一个 REST 服务时,你可以设置一个BindingMode以便 Camel 会根据传入的 Content-Type 自动从 JSON 或 XML 解组:

restConfiguration()
    .bindingMode(RestBindingMode.auto) // can also be .xml, .json,....
    .component("servlet");

但是,我还没有看到这个功能被使用或暴露在 REST 服务之外。

如果您有兴趣,实现在RestBindingAdvice中。


推荐阅读