要使用mapstruct映射某个对象,我需要一些需要额外参数来进行自定义后处理的工作:
@Mapper
public abstract class AlertConfigActionMapper {
@Mappings({ @Mapping(target = "label", ignore = true)})
public abstract AlertConfigActionTO map (AlertConfigAction action, Locale userLanguage);
@AfterMapping
public void setLabel (AlertConfigAction action, @MappingTarget AlertConfigActionTO to, Locale userLanguage) {
for (AlertConfigActionLabel label : action.getAlertConfigActionLabels()) {
if (label.getLanguage().equals(userLanguage)) {
to.setLabel(label.getLabel());
break;
} else if (label.getLanguage().equals(Locale.ENGLISH)) {
to.setLabel(label.getLabel());
}
}
}
}
这工作很好.
当我将以下方法添加到此映射器时,问题开始:
public abstract ArrayList mapList (List actions, Locale userLanguage);
我需要传递这个参数(userLanguage),但是在这种情况下,mapstruct似乎是“分解”的:我为这个部分生成以下代码(这自然会给出一个编译错误):
@Override
public List mapList(List actions, Locale userLanguage) {
if ( actions == null && userLanguage == null ) {
return null;
}
List list = new List();
return list;
}
我确定它与参数相关,因为如果我删除它(从所有映射方法),那么mapList方法是正确生成的.
在这种情况下允许自定义参数需要做什么?