簡體   English   中英

如何告訴 MapStruct “不要”使用 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"
    }

如果您想禁用使用構建器,您可以通過將@Builder(disableBuilder = true)到您的@Mapper@Mapper

例如

@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
class UserRestMapper {
    public abstract UserDto map(User obj);

}

注意@Builder來自org.mapstruct

不知道禁用它,但為什么不這樣做呢?

@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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM