372.3. Java DSL 的基本使用情况
372.3.1. 明确实例化数据格式
只需从软件包 org.apache.camel.dataformat.xmljson 实例化 XmlJsonDataFormat。请确定您安装了 camel-xmljson 功能(如果在 OSGi 上运行),或者您已在类路径中包含 camel-xmljson-7.9.jar 及其传递的依赖关系。使用默认配置进行初始化示例:
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
要根据以上选项调整数据格式的行为,请使用适当的集合:
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setEncoding("UTF-8");
xmlJsonFormat.setForceTopLevelObject(true);
xmlJsonFormat.setTrimSpaces(true);
xmlJsonFormat.setRootName("newRoot");
xmlJsonFormat.setSkipNamespaces(true);
xmlJsonFormat.setRemoveNamespacePrefixes(true);
xmlJsonFormat.setExpandableProperties(Arrays.asList("d", "e"));
在实例化数据格式后,下一步实际上会从 marshal () 和 unmarshal () DSL 元素中使用它:
// from XML to JSON
from("direct:marshal").marshal(xmlJsonFormat).to("mock:json");
// from JSON to XML
from("direct:unmarshal").unmarshal(xmlJsonFormat).to("mock:xml");