如何将`this`传递给Dozer字段映射?

时间:2012-03-11 16:13:56

标签: java dozer

在我的应用程序中,我有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>

MyConverterConfigurableCustomConverter的实例:

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;
    }
}

为什么在源内评论中发现了事情?

1 个答案:

答案 0 :(得分:4)

您需要告诉推土机地图的哪个键应该映射到b.anotherField,类似

<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