1

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?

David Conrad
  • 15,432
  • 2
  • 42
  • 54
Naou
  • 726
  • 1
  • 10
  • 23
  • Does this answer your question? [MapStruct and Lombok not working together](https://stackoverflow.com/questions/47676369/mapstruct-and-lombok-not-working-together) – Filip Jan 03 '21 at 07:04
  • @Filip No, the `annotationProcessorPaths` are not missing, the problem is that `lombok-mapstruct-binding` is needed for Lombok 1.18.16 and later. – David Conrad Jan 05 '21 at 23:10
  • I know what the problem is. Did you check the edits of the answer? It says explicitly that you need to add `lombok-mapstruct-binding` from Lombok 1.18.16 – Filip Jan 06 '21 at 07:45
  • @Filip Ah, no, I just saw that it was talking about Lombok 1.16.14 and missing Lombok from the annotation processors. I see the later edit now. – David Conrad Jan 06 '21 at 15:30

1 Answers1

5

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>
David Conrad
  • 15,432
  • 2
  • 42
  • 54