将Java 8可选与Mapstruct一起使用

问题描述 投票:0回答:1

我有这两门课:

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都可以

java mapstruct
1个回答
0
投票

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.