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>