I'm using the latest versions of Spring boot above 2.2.5 and Java 15, lombok, Mapstruct for mapping some pojos.
When using version 2.2.5 of spring boot I'm getting the expected result :
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-01-02T21:59:24+0100",
comments = "version: 1.4.1.Final, compiler: javac, environment: Java 15.0.1 (Oracle Corporation)"
)
@Component
public class ProduitMapperImpl implements ProduitMapper {
@Override
public ProduitDtoResponse produitToProduitDtoResponse(Produit produit) {
if ( produit == null ) {
return null;
}
ProduitDtoResponseBuilder produitDtoResponse = ProduitDtoResponse.builder();
produitDtoResponse.id( produit.getId() );
produitDtoResponse.nom( produit.getNom() );
produitDtoResponse.prix( produit.getPrix() );
return produitDtoResponse.build();
}
@Override
public Produit produitToProduitDtoResponse(ProduitDtoResponse produitDto) {
if ( produitDto == null ) {
return null;
}
ProduitBuilder produit = Produit.builder();
produit.id( produitDto.getId() );
produit.nom( produitDto.getNom() );
produit.prix( produitDto.getPrix() );
return produit.build();
}
}
But when using Version 2.4 and above, there's no builder, instead I have the java new keyword and not the other fields, I'm getting :
Here's the maven pom conf that's not working : BOM
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-01-02T22:05:16+0100",
comments = "version: 1.4.1.Final, compiler: javac, environment: Java 15.0.1 (Oracle Corporation)"
)
@Component
public class ProduitMapperImpl implements ProduitMapper {
@Override
public ProduitDtoResponse produitToProduitDtoResponse(Produit produit) {
if ( produit == null ) {
return null;
}
ProduitDtoResponse produitDtoResponse = new ProduitDtoResponse();
return produitDtoResponse;
}
@Override
public Produit produitToProduitDtoResponse(ProduitDtoResponse produitDto) {
if ( produitDto == null ) {
return null;
}
Produit produit = new Produit();
return produit;
}
}
The only thing I change is Spring Boot version and it work as expected, is there any workaround or issues with version 2.4 and above of Spring Boot, please?
Your problem is that because of this in your pom.xml
:
<version>${lombok.version}</version>
you are getting the Lombok version from the parent pom of your parent pom, spring-boot-starter-parent
(your grandparent pom?), and thus with Spring Boot 2.4.0 you are picking up a newer version of Lombok.
Starting with Lombok 1.18.16, you need to add another dependency. See this answer in the MapStruct FAQ:
If you are using Lombok 1.18.16 or newer you also need to add lombok-mapstruct-binding in order to make Lombok and MapStruct work together.
They have a sample project to demonstrate how to use them together. Here are the relevant parts of the sample pom.xml
:
<properties>
<!-- . . . -->
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
<!-- . . . -->
<annotationProcessorPaths>
<!-- . . . -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>