Home >Java >javaTutorial >How to Resolve MapStruct\'s \'Unknown Property\' Error When Used with Lombok?

How to Resolve MapStruct\'s \'Unknown Property\' Error When Used with Lombok?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-23 16:28:18570browse

How to Resolve MapStruct's

MapStruct and Lombok Compatibility Issue: Unknown Property in Result Type

Problem Statement:

When using both MapStruct and Lombok together, an error occurs stating "Unknown property" in the result type. This issue arises when MapStruct cannot find a property in the target DTO class due to Lombok annotations removing getters and setters.

Cause:

Maven is using the MapStruct processor but not the Lombok processor. The annotationProcessorPaths in the Maven configuration specifies which processors should be used.

Solution:

Option 1: Add Lombok Dependency to AnnotationProcessorPaths

Modify the Maven compiler plugin configuration to include both the Lombok and MapStruct annotation processors:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${org.projectlombok.version}</version>
            </path>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

For Lombok versions 1.18.16 and above, also add the lombok-mapstruct-binding dependency:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-mapstruct-binding</artifactId>
    <version>0.2.0</version>
</dependency>

Option 2: Add MapStruct Processor as Dependency

Add the mapstruct-processor dependency to your dependencies and remove the annotationProcessorPaths:

<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>

Additional Note:

To ensure annotation processing in IntelliJ, add the mapstruct-processor as a provided dependency due to a known issue (IDEA-150621).

The above is the detailed content of How to Resolve MapStruct\'s \'Unknown Property\' Error When Used with Lombok?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn