Reputation: 141
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
Upvotes: 3
Views: 2917
Reputation: 8052
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>
Upvotes: 5