How to pass `this` to Dozer field mapping?

岁酱吖の 提交于 2019-12-22 13:51:49

问题


In my app I have Dozer mapping which looks like this:

<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 is an instance of 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;
    }
}

Why things noted in in-source comment happen?


回答1:


You need to tell dozer which key of the Map should be mapped to b.anotherField, with something like

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

See http://dozer.sourceforge.net/documentation/mapbackedproperty.html#Mapping_Class_Level_Properties_to_a_java.util.Map_or_a_Custom_Map_with_unique_GetSet_methods



来源:https://stackoverflow.com/questions/9656606/how-to-pass-this-to-dozer-field-mapping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!