在我的应用程序中,我具有如下所示的推土机映射:
<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;
}
}
为什么发生在源注释中记录的事情?
最佳答案
您需要告知推土机应该将Map的哪个键映射到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