我有以下类和映射器来映射它们。如何将 Mapstruct 配置为“不”使用 Lombok 构建器?(不删除@Builder注释)?当使用最新版本的Lombok和mapstruct时,mapstruct在使用@Builder注解时会自动使用Builder。我找不到禁用它的方法,因为我需要 @AfterMapping 方法中的实例,因为构建器没有公开所有必需的方法(在此用例中不允许 @SuperBuilder)
@Entity(name = "user_details")
@Data
@Builder
public class User extends AuditableEntityBase {
@Version
@NotNull
private Integer version;
@NotNull
private String name;
@NotNull
private String email;
@NotNull
private Address address; // Just another Class containing another class that is mapped as well.
}
@Value
@Builder
public class UserDto extends AuditableEntityBaseDto {
@NotNull
private Integer version;
@NotNull
private String name;
@NotNull
private String email;
@NotNull
private AddressDto address;
}
@Mapper(componentModel = "spring")
class UserRestMapper {
public abstract UserDto map(User obj);
}
@AfterMapping
public void decorate(User source, @MappingTarget AuditableEntityBase target) {
// Method is never called.
// Method is called in case the second argument is: "@MappingTarget UserDto.UserDtoBuilder target"
}
Run Code Online (Sandbox Code Playgroud)
Fil*_*lip 22
如果您想禁用使用构建器,您可以通过添加@Builder(disableBuilder = true)到您的@Mapper.
例如
@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
class UserRestMapper {
public abstract UserDto map(User obj);
}
Run Code Online (Sandbox Code Playgroud)
注意 @Builder是来自org.mapstruct包装