我有这两门课:
public class CustomerEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String firstName;
private String lastName;
private String address;
private int age;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
和
public class CustomerDto {
private Long customerId;
private String firstName;
private String lastName;
private Optional<String> address;
private int age;
}
问题是Mapsruct无法识别可选变量“地址”。
任何人都有解决问题的方法,让Mapstic都可以
Mapstruct尚不支持此功能。他们的Githib上有一张打开的票证,要求此功能:https://github.com/mapstruct/mapstruct/issues/674
解决此问题的一种方法已添加到同一票证的注释中:https://github.com/mapstruct/mapstruct/issues/674#issuecomment-378212135
@Mapping(source = "child", target = "kid", qualifiedByName = "unwrap")
Target map(Source source);
@Named("unwrap")
default <T> T unwrap(Optional<T> optional) {
return optional.orElse(null);
}