Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell MapStruct "not" to use the Lombok Builder?

Tags:

java

mapstruct

I have the following classes and mapper to map them. How can I configure Mapstruct to "not" use the Lombok builder? (without removing the @Builder annotation)? When using the latest version of Lombok and mapstruct, mapstruct will automatically use the Builder when the @Builder annotation is used. I can not find a way to disable that, as I need the Instance in an @AfterMapping method as the builder doesn't expose all required methods (@SuperBuilder is not allowed in this use case)

@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"
    }
like image 340
edbras Avatar asked Dec 07 '25 06:12

edbras


1 Answers

If you want to disable using builders you can do so by adding @Builder(disableBuilder = true) to your @Mapper.

e.g.

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

}

Note @Builder is from the org.mapstruct package

like image 173
Filip Avatar answered Dec 08 '25 20:12

Filip