正在使用Tech Stack:
Java 8 MapStruct:1.2.0.Final 龙目岛:1.16.18 IDE:已安装IntelliJ - Lombok插件
@Getter
和@Setter
注释时,我遇到了问题,mapstruct
无法找到该属性并说:Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"?
< / LI>
以下是项目文件:
实体对象: One.java :
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class One {
private int id;
private Integer version;
private int projectId;
private String title;
private String code;
private int sortOrder;
}
DTO对象: OneDTO.java :
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class OneDto {
private int id;
private Integer version;
private int projectId;
private String title;
private String code;
private int sortOrder;
}
Mapper类: OneMapper.java
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.vg.once.dto.OneDto;
import com.vg.once.entity.One;
@Mapper
public interface OneMapper {
@Mapping(target="id", source="one.id")
OneDto createOne (One one);
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vg</groupId>
<artifactId>mapstruct</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Mapstruct-test</name>
<properties>
<java.version>1.8</java.version>
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
<org.projectlombok.version>1.16.18</org.projectlombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.18.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>src/main/java</sourceDirectory>
<addOutputDirectory>false</addOutputDirectory>
<outputDirectory>${project.build.directory}/delombok</outputDirectory>
<encoding>UTF-8</encoding>
<skip>false</skip>
<verbose>false</verbose>
</configuration>
</plugin>
<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.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
构建跟踪:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Mapstruct-test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- lombok-maven-plugin:1.16.18.1:delombok (default) @ mapstruct ---
[INFO] Delombok complete.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mapstruct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ mapstruct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 5 source files to /home/vivekgupta/Documents/workspaces/mapstruct-test/mapstruct/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/vivekgupta/Documents/workspaces/mapstruct-test/mapstruct/src/main/java/com/vg/once/mapper/OneMapper.java:[12,9] Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.637 s
[INFO] Finished at: 2017-12-06T19:23:53+05:30
[INFO] Final Memory: 19M/235M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project mapstruct: Compilation failure
[ERROR] /home/vivekgupta/Documents/workspaces/mapstruct-test/mapstruct/src/main/java/com/vg/once/mapper/OneMapper.java:[12,9] Unknown property "id" in result type com.vg.once.dto.OneDto. Did you mean "null"?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
请分享如何使用Lombok和MapStruct一起使用?
答案 0 :(得分:34)
它不起作用的原因是因为Maven只使用MapStruct处理器而不使用Lombok处理器。 annotationProcessorPaths
告诉maven它应该使用哪些处理器。
delombok什么都不做,因为你最后每个类有2个文件,我认为maven编译器看不到它们。
您有两个选择:
选项1:在anootationProcessorPaths
<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.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
选项2:
将mapstruct-processor
依赖项添加到依赖项中,然后删除annotationProcessorPaths
。这样,maven编译器将获取依赖项中的所有注释处理器。
我建议使用选项1,因为您可以确定您没有在代码中使用某些MapStruct传递依赖项和内部类。
编辑:
为确保IntelliJ注释处理也有效,您必须将mapstruct-processor
添加为由IDEA-150621引起的provided
依赖关系。 IntelliJ目前不使用Maven的annotationProcessorPaths
来正确配置项目。
答案 1 :(得分:9)
以防万一有人在寻找如何使用Gradle进行配置的方法:
dependencies {
// Lombok
compileOnly 'org.projectlombok:lombok:1.18.2'
annotationProcessor 'org.projectlombok:lombok:1.18.2'
// MapStruct
compileOnly 'org.mapstruct:mapstruct-jdk8:1.2.0.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.2.0.Final'
}
答案 2 :(得分:2)
在我的 Spring Boot Maven 项目中多次尝试后,以下终于奏效了:
pom.xml 的配置:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<version.lombok>1.18.18</version.lombok>
<version.mapstruct>1.4.2.Final</version.mapstruct>
<version.mapstruct-lombok>0.2.0</version.mapstruct-lombok>
</properties>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${version.lombok}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${version.mapstruct}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${version.mapstruct-lombok}</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>9</source>
<target>9</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${version.mapstruct}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${version.lombok}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${version.mapstruct-lombok}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
希望这有帮助,我之前的映射器没有映射字段值,但现在这些是源和目标之间的映射器 + 每个元素的嵌套列表也与字段值一起映射。
答案 3 :(得分:2)
如果有人在使用最新的 Lombok 和 Mapstruct 库版本时阅读本文:-
如果您使用的是 Lombok 1.18.16 或更新版本,那么您还需要添加 lombok-mapstruct-binding
以使 Lombok 和 MapStruct 协同工作。
https://mapstruct.org/faq/#Can-I-use-MapStruct-together-with-Project-Lombok
答案 4 :(得分:1)
如果仍然有人在寻找这个答案,那么我遇到了JOOQ + MapStruct Integration的类似问题。答案是路径的顺序重要
我的构建片段
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<!-- This is needed when using Lombok 1.8.16 and above -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${org.lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
答案 5 :(得分:1)
对于将 Intellij 升级到 2020.3 版本后的问题,以下解决方案对我有用。 使用 lombok 版本:1.18.16,lombok-mapstruct-binding:0.2.0
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</dependency>
在插件的注解处理部分更改lombok和mapstruct插件路径的顺序:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>11</source>
<target>11</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>
</plugins>
答案 6 :(得分:0)
我有类似的问题。原来我的MapStruct版本已经过时了!
我使用的是MapStruct版本1.1.0.Final,但是 Lombok支持至少1.2.0.Final是必需的。
答案 7 :(得分:0)
对我来说,解决方案非常简单。
处理器的顺序很重要。
在我的情况下,mapstruct处理器在lombok处理器之前定义。如果顺序不好,则mapstruct不会生成映射,只需创建结果类的实例(我在生成的实现中看到了该实例)。
我的插件配置正确无误:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>15</source>
<target>15</target>
<compilerArgs>--enable-preview</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
当然,您需要添加依赖项:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
答案 8 :(得分:0)
我同意该命令在使用gradle时也会发挥作用。就我而言,在多模块设置中将subprojects { ... dependencyManagement{}}
添加到父build.gradle
时,它失败了。
答案 9 :(得分:0)
mapstruct只是返回一个空实例。最近,我将Intellij版本升级到2020.3,并启动了Spring Boot 2.4.0,并进入了这种情况。
当我将Spring版本更改为2.3.o.RELEASE时,我看到编译后是mapstruct正在正确构建映射。
仔细阅读mapstruct最近更新的帖子..,但这需要时间.. :)所以,就目前而言,我从maven转到Gradle项目,并且一切正常。.听起来很奇怪,但确实如此... < / p>
如果有人能发表正确的问题和解决方案,我将不胜感激。