如何告诉 MapStruct "not" 使用 Lombok Builder?
How to tell MapStruct "not" to use the Lombok Builder?
我有以下 类 和映射器来映射它们。
如何将 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"
}
不知道如何禁用它,但为什么不这样做呢?
@Mapper(componentModel = "spring")
abstract class UserRestMapper {
public abstract UserDto map(User obj);
public UserDto decoratedMap(User obj) {
UserDto mapped = map(obj);
// your after mapping logic
return mapped;
}
}
如果您想禁用构建器,您可以将 @Builder(disableBuilder = true)
添加到您的 @Mapper
。
例如
@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
class UserRestMapper {
public abstract UserDto map(User obj);
}
注意 @Builder
来自 org.mapstruct
包
我有以下 类 和映射器来映射它们。 如何将 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"
}
不知道如何禁用它,但为什么不这样做呢?
@Mapper(componentModel = "spring")
abstract class UserRestMapper {
public abstract UserDto map(User obj);
public UserDto decoratedMap(User obj) {
UserDto mapped = map(obj);
// your after mapping logic
return mapped;
}
}
如果您想禁用构建器,您可以将 @Builder(disableBuilder = true)
添加到您的 @Mapper
。
例如
@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
class UserRestMapper {
public abstract UserDto map(User obj);
}
注意 @Builder
来自 org.mapstruct
包