2

私のアプリでは、次のような Dozer マッピングがあります。

<mapping>
    <class-a>java.util.HashMap</class-a>
    <class-b>org.mycompany.TargetClass</class-b>
    <field custom-converter="org.example.MyConverter">
        <a>this</a>
        <b>anotherField</b>
    </field>
</mapping>

MyConverterのインスタンスですConfigurableCustomConverter:

public class MyConverter implements ConfigurableCustomConverter {

    private String parameter;

    @Override
    public Object convert(
            Object existingDestinationFieldValue,
            Object sourceFieldValue,
            Class<?> destinationClass,
            Class<?> sourceClass) {
        // sourceClass is always java.lang.Object and
        // sourceFieldValue is always null!!!
    }

    @Override
    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
}

ソース内のコメントに記載されていることがなぜ起こるのですか?

4

1 に答える 1

4

b.anotherField次のような方法で、マップのどのキーにマップする必要があるかを dozer に伝える必要があります。

<field custom-converter="org.example.MyConverter">
    <a key="foobar">this</a>
    <b>anotherField</b>
</field>

http://dozer.sourceforge.net/documentation/mapbackedproperty.html#Mapping_Class_Level_Properties_to_a_java.util.Map_or_a_Custom_Map_with_unique_GetSet_methodsを参照してください。

于 2012-03-11T18:28:20.167 に答える