【发布时间】:2021-12-09 06:13:24
【问题描述】:
如何映射以下内容:
class Source {
String name;
List<Other> others;
}
class Other {
String otherName;
List<More> mores;
}
class More {
String moreName;
}
class Target {
String name;
String otherName;
String moreName;
}
希望它可能是这样的:
@Mapper
public interface MyMapper {
@Mapping(target = "name", source = "name")
@Mapping(target = "otherName", source = "others[0].otherName")
@Mapping(target = "morename", source = "others[0].mores[0].moreName")
Target map(Source source);
我看到 'expression = "java(others.get(0).otherName)"' 可以使用,但是使用表达式时没有空检查,这可能会导致 NullPointeException。可以应用任何带有空检查和默认值的建议吗?
【问题讨论】:
标签: java spring spring-boot mapstruct