Red Hat Training

A Red Hat training course is available for Red Hat Fuse

85.5. 拡張機能のマッピング

Dozer コンポーネントは、カスタムコンバーターとして Dozer マッピングフレームワークに多数の拡張機能を実装します。 これらのコンバーターは、Dozer 自体によって直接サポートされていないマッピング機能を実装します。

85.5.1. 変数のマッピング

変数マッピングを使用すると、ソースフィールドの値を使用する代わりに、Dozer 設定内の変数定義の値をターゲットフィールドにマッピングできます。 これは、ターゲットフィールドにリテラル値を割り当てることができる他のマッピングフレームワークでの定数マッピングと同等です。 変数マッピングを使用するには、マッピング設定内で変数を定義し、VariableMapper クラスから選択したターゲットフィールドにマップするだけです。

<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
  <configuration>
    <variables>
      <variable name="CUST_ID">ACME-SALES</variable>
    </variables>
  </configuration>
  <mapping>
    <class-a>org.apache.camel.component.dozer.VariableMapper</class-a>
    <class-b>org.example.Order</class-b>
    <field custom-converter-id="_variableMapping" custom-converter-param="${CUST_ID}">
      <a>literal</a>
      <b>custId</b>
    </field>
  </mapping>
</mappings>

85.5.2. カスタムマッピング

カスタムマッピングを使用すると、ソースフィールドをターゲットフィールドにマップする方法について独自のロジックを定義できます。 これらは Dozer のカスタマーコンバーターと機能が似ていますが、次の 2 つの顕著な違いがあります。

  • カスタムマッピングを使用して、1 つのクラスに複数のコンバーターメソッドを含めることができます。
  • カスタムマッピングを使用して Dozer 固有のインターフェイスを実装する必要はありません。

カスタムマッピングは、マッピング設定で組み込みの '_customMapping' コンバーターを使用して宣言されます。 このコンバーターのパラメーターの構文は次のとおりです。

[class-name][,method-name]

メソッド名はオプションです。Dozer コンポーネントは、マッピングに必要な入力と出力のタイプに一致するメソッドを検索します。 カスタムマッピングと設定の例を以下に示します。

public class CustomMapper {
    // All customer ids must be wrapped in "[ ]"
    public Object mapCustomer(String customerId) {
        return "[" + customerId + "]";
    }
} 
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
  <mapping>
    <class-a>org.example.A</class-a>
    <class-b>org.example.B</class-b>
    <field custom-converter-id="_customMapping"
        custom-converter-param="org.example.CustomMapper,mapCustomer">
      <a>header.customerNum</a>
      <b>custId</b>
    </field>
  </mapping>
</mappings>

85.5.3. 式のマッピング

式マッピングを使用すると、Camel の強力な 言語 機能を使用して式を評価し、結果をマッピングのターゲットフィールドに割り当てることができます。 Camel がサポートする任意の言語を式マッピングで使用できます。 式の基本的な例には、Camel メッセージヘッダーまたはエクスチェンジプロパティーをターゲットフィールドにマップする機能や、複数のソースフィールドをターゲットフィールドに連結する機能が含まれます。 マッピング式の構文は次のとおりです。

[language]:[expression]

メッセージヘッダーをターゲットフィールドにマッピングする例:

<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
  <mapping>
    <class-a>org.apache.camel.component.dozer.ExpressionMapper</class-a>
    <class-b>org.example.B</class-b>
    <field custom-converter-id="_expressionMapping" custom-converter-param="simple:\${header.customerNumber}">
      <a>expression</a>
      <b>custId</b>
    </field>
  </mapping>
</mappings>

Dozer が EL を使用して定義された変数値を解決しようとするときのエラーを防ぐために、式内のすべてのプロパティーを \ でエスケープする必要があることに注意してください。