Home >Java >javaTutorial >Why Does MapStruct Fail to Recognize Lombok\'s @Getter and @Setter Annotations, and How Can I Fix It?

Why Does MapStruct Fail to Recognize Lombok\'s @Getter and @Setter Annotations, and How Can I Fix It?

Barbara Streisand
Barbara StreisandOriginal
2024-11-24 19:38:49773browse

Why Does MapStruct Fail to Recognize Lombok's @Getter and @Setter Annotations, and How Can I Fix It?

Unknown @Getter and @Setter Annotations in MapStruct Mappings with Lombok

Problem Description

Integrating MapStruct and Lombok can lead to the error "Unknown property ... in result type ..." when using @Getter and @Setter annotations on entity and DTO classes together. This occurs despite the versions of both libraries being compatible.

Solution

The issue arises because Maven uses the MapStruct processor exclusively, ignoring the Lombok one. To resolve this:

Option 1: Add Lombok Dependency to Annotation Processor Path

In the Maven compiler plugin configuration, add the Lombok dependency to the annotationProcessorPaths list:

<annotationProcessorPaths>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${org.projectlombok.version}</version>
    </path>
    ...
</annotationProcessorPaths>

Additionally, for Lombok 1.18.16 and above, add the following path:

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

Option 2: Add MapStruct Processor Dependency

Alternatively, add the MapStruct processor as a dependency and remove the annotationProcessorPaths:

<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
</dependencies>
NOTE: Option 1 is recommended to ensure that no internal MapStruct classes are referenced in the code.

Intellij Configuration

To enable annotation processing in IntelliJ, add the MapStruct processor as a provided dependency due to [IntelliJ issue IDEA-150621](https://youtrack.jetbrains.com/issue/IDEA-150621).

The above is the detailed content of Why Does MapStruct Fail to Recognize Lombok\'s @Getter and @Setter Annotations, and How Can I Fix It?. 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