ModelMapper 将数组 属性 (get(0)) 展平为字符串?
ModelMapper flatten array property (get(0)) to String?
Src 对象有一个 属性:
private List<Pojo> goals;
Dest 对象有一个 属性
private String goal;
我想映射 Src.goals.get(0).getName() -> Dest.goal。目标将始终包含一个项目,但必须将其作为列表拉入,因为它来自 Neo4j。
我试过:
userTypeMap.addMappings(mapper -> {
mapper.map(src -> src.getGoals().get(0).getName(), UserDto::setGoal);
});
但是 modelmapper 不喜欢这个参数。然后我试了:
userTypeMap.addMappings(mapper -> {
mapper.map(src -> src.getGoals(), UserDto::setGoal);
});
这给了我:
"goal": "[org.xxx.models.Goal@5e0b5bd8]",
然后我尝试为 List -> String 添加一个转换器,但没有被调用。如果我将整个 pojo 的转换器添加到 dto,那么我必须映射我不想做的整个 pojo,我只想覆盖这个 属性.
您可以将 List
访问包装在 Converter
中,然后在 PropertyMap
中使用它,如下所示:
ModelMapper mm = new ModelMapper();
Converter<List<Pojo>, String> goalsToName =
ctx -> ctx.getSource() == null ? null : ctx.getSource().get(0).getName();
PropertyMap<Src, Dest> propertyMap = new PropertyMap<>() {
@Override
protected void configure() {
using(goalsToName).map(source.getGoals()).setGoal(null);
}
};
mm.addMappings(propertyMap);
我不太确定你想用这个来避免什么:
If I add a converter for the entire pojo to dto then I have to map the whole pojo which I don't want to do, I just want to override this one property.
如果您需要映射整个 "pojo" 或只需要映射一个字段,请创建一个转换器,例如:
Converter<HasListOfPojos, HasOnePojo> x = new Converter<>() {
ModelMapper mm2 = new ModelMapper();
@Override
public HasOnePojo convert(MappingContext<HasListOfPojos, HasOnePojo> context) {
// do not create a new mm2 and do this mapping if no need for other
// fields, just create a new "hop"
HasOnePojo hop = mm2.map(context.getSource(), HasOnePojo.class);
// here goes the get(0) mapping
hop.setGoal(context.getSource().getGoals().get(0));
return hop;
}
};
Src 对象有一个 属性:
private List<Pojo> goals;
Dest 对象有一个 属性
private String goal;
我想映射 Src.goals.get(0).getName() -> Dest.goal。目标将始终包含一个项目,但必须将其作为列表拉入,因为它来自 Neo4j。
我试过:
userTypeMap.addMappings(mapper -> {
mapper.map(src -> src.getGoals().get(0).getName(), UserDto::setGoal);
});
但是 modelmapper 不喜欢这个参数。然后我试了:
userTypeMap.addMappings(mapper -> {
mapper.map(src -> src.getGoals(), UserDto::setGoal);
});
这给了我:
"goal": "[org.xxx.models.Goal@5e0b5bd8]",
然后我尝试为 List -> String 添加一个转换器,但没有被调用。如果我将整个 pojo 的转换器添加到 dto,那么我必须映射我不想做的整个 pojo,我只想覆盖这个 属性.
您可以将 List
访问包装在 Converter
中,然后在 PropertyMap
中使用它,如下所示:
ModelMapper mm = new ModelMapper();
Converter<List<Pojo>, String> goalsToName =
ctx -> ctx.getSource() == null ? null : ctx.getSource().get(0).getName();
PropertyMap<Src, Dest> propertyMap = new PropertyMap<>() {
@Override
protected void configure() {
using(goalsToName).map(source.getGoals()).setGoal(null);
}
};
mm.addMappings(propertyMap);
我不太确定你想用这个来避免什么:
If I add a converter for the entire pojo to dto then I have to map the whole pojo which I don't want to do, I just want to override this one property.
如果您需要映射整个 "pojo" 或只需要映射一个字段,请创建一个转换器,例如:
Converter<HasListOfPojos, HasOnePojo> x = new Converter<>() {
ModelMapper mm2 = new ModelMapper();
@Override
public HasOnePojo convert(MappingContext<HasListOfPojos, HasOnePojo> context) {
// do not create a new mm2 and do this mapping if no need for other
// fields, just create a new "hop"
HasOnePojo hop = mm2.map(context.getSource(), HasOnePojo.class);
// here goes the get(0) mapping
hop.setGoal(context.getSource().getGoals().get(0));
return hop;
}
};