Home > Java > javaTutorial > How to Fix \'Unknown Property\' Errors When Using MapStruct with Lombok?

How to Fix \'Unknown Property\' Errors When Using MapStruct with Lombok?

Susan Sarandon
Release: 2024-12-06 05:21:09
Original
471 people have browsed it

How to Fix

MapStruct and Lombok Integration: Resolving the "Unknown Property" Error

When using MapStruct with Lombok annotations, you may encounter an error stating "Unknown property in result type." This error arises because Maven only employs the MapStruct processor and not the Lombok processor.

Option 1: Modify the Maven Compiler Plugin

Update the maven-compiler-plugin configuration to include both the Lombok and MapStruct annotation processor paths:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.6.1</version>
  <configuration>
    <annotationProcessorPaths>
      <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${org.projectlombok.version}</version>
      </path>
      <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-mapstruct-binding</artifactId>
        <version>0.2.0</version>
      </path>
      <path>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${org.mapstruct.version}</version>
      </path>
    </annotationProcessorPaths>
  </configuration>
</plugin>
Copy after login

Option 2: Declare MapStruct as a Dependency

Alternatively, add the mapstruct-processor dependency to the main dependencies section and remove the annotationProcessorPaths:

<dependency>
  <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct-processor</artifactId>
  <version>${org.mapstruct.version}</version>
</dependency>
Copy after login

Additional Considerations for IntelliJ

To prevent IntelliJ errors, add the mapstruct-processor as a provided dependency:

<dependency>
  <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct-processor</artifactId>
  <version>${org.mapstruct.version}</version>
  <scope>provided</scope>
</dependency>
Copy after login

Note for Lombok Versions

For Lombok versions 1.18.16 and above, you will require the lombok-mapstruct-binding dependency to ensure proper integration with MapStruct.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template