I am trying to use mapstruct in my Play 2.4 Java8 JPA project. Steps I have done:
Added Dependency
"org.mapstruct" % "mapstruct-jdk8" % "1.1.0.Beta1",
"org.mapstruct" % "mapstruct-processor" % "1.1.0.Beta1"
Model
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String fullName;
private String email;
}
EmployeeDto
public class EmployeeDto {
private String full_name;
private String email;
}
EmployeeMapper
@Mapper
public interface EmployeeMapper {
EmployeeMapper INSTANCE = Mappers.getMapper(EmployeeMapper.class);
@Mapping(source = "fullName", target = "full_name")
EmployeeDto employeeToEmployeeDto(Employee employee);
}
But its giving me a compilation error
error: Unknown property "full_name" in return type.
[error] @Mapping(source = "fullName", target = "full_name")
What could be the issue for the error?
Additional case for other developers: If you use Lombok, maven uses only the MapStruct processor. So Lombok cannot generate the getters/setters. To resolve that, add the lombok dependency in the annotationProcessorPaths
.
Also if you use Lombok 1.8.16 and above, you have to add lombok-mapstruct-binding
too.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${projectlombok.version}</version>
</path>
<!-- This is needed when using Lombok 1.8.16 and above -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.1.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With