Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapstruct seems not recognize Lombok's @SuperBuilder

I have two domain classes like these.

@Getter
@Setter
public class User {
    private String name;
}
@Getter
@Setter
public class Student extends User {
    private int grade;
}

And I have also two Dto classes like these.

@Getter
@SuperBuilder
public class UserDto {
    private final String name;
}
@Getter
@SuperBuilder
public class StudentDto extends UserDto {
    private final int grade;
}

So I made a mapper class StudentMapper which extends GenericMapper.

public interface GenericMapper<D, E> {
    D toDto(E e);
    E toEntity(D d);
}
@Mapper(componentModel = "spring")
public interface StudentMapper extends GenericMapper<StudentDto, Student> {
}

But I got an error while compiling Mapper.

"StudentDto does not have an accessible constructor."

What's wrong with those codes?

I want StudentDto to be unmodifiable. What will be the best way to get there with lombok and mapstruct?

You can get the source codes from here. https://github.com/jangdaewon/sandbox.lombokmapstruct

like image 442
Shinbop Avatar asked Nov 08 '25 01:11

Shinbop


1 Answers

It's a bit counter-intuitive, but Lombok has to be put after mapstruct in the list of annotation processors. Mapstruct detects Lombok and waits until Lombok has done its job completely (Lombok may need several annotation processing rounds).

So simply change the order of your processors in the <configuration> of maven-compiler-plugin in your pom.xml like this:

<annotationProcessorPaths>
    <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </path>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
    </path>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-mapstruct-binding</artifactId>
        <version>${lombok-mapstruct-binding.version}</version>
    </path>
</annotationProcessorPaths>
like image 197
Jan Rieke Avatar answered Nov 09 '25 19:11

Jan Rieke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!