343.2. Java DSL の基本的な使い方
343.2.1. データ形式の明示的なインスタンス化
org.apache.camel.dataformat.xmljson
パッケージの XmlJsonDataFormat
をインスタンス化するだけです。camel-xmljson
機能 (OSGi で実行している場合) がインストールされていること、または camel-xmljson-7.2.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");
343.2.2. インラインでデータ形式を定義する
または、xmljson()
DSL 要素を使用してインラインでデータ形式を定義できます。
// from XML to JSON - inline dataformat from("direct:marshalInline").marshal().xmljson().to("mock:jsonInline"); // from JSON to XML - inline dataformat from("direct:unmarshalInline").unmarshal().xmljson().to("mock:xmlInline");
必要に応じて、Map<String, String>
をインラインメソッドに渡して、カスタムオプションを提供することもできます。
Map<String, String> xmlJsonOptions = new HashMap<String, String>(); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ENCODING, "UTF-8"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ROOT_NAME, "newRoot"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.SKIP_NAMESPACES, "true"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.REMOVE_NAMESPACE_PREFIXES, "true"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.EXPANDABLE_PROPERTIES, "d e"); // from XML to JSON - inline dataformat w/ options from("direct:marshalInlineOptions").marshal().xmljson(xmlJsonOptions).to("mock:jsonInlineOptions"); // form JSON to XML - inline dataformat w/ options from("direct:unmarshalInlineOptions").unmarshal().xmljson(xmlJsonOptions).to("mock:xmlInlineOptions");