问题描述
我有两节课。型号:
@Data
public class ExampleModel {
private String someField;
private String fieldInModel;
}
和DTO:
@Data
public class ExampleDto {
private String someField;
private String fieldindto;
}
我要配置简单的映射器(具有不同的字段名称):
@Mapper(componentModel = "spring")
public interface ExampleMapper {
@Mapping(target = "fieldInModel",source = "fieldindto")
ExampleModel mapToExample(ExampleDto exampleDto);
}
我的项目是一个简单的Spring Initializr,在其中我为Lombok和MapStruct添加了配置:
...
<properties>
<java.version>11</java.version>
<mapstruct.version>1.4.1.Final</mapstruct.version>
<lombok.version>1.18.16</lombok.version>
</properties>
<dependencies>
...
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<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.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
<release>11</release>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
当我尝试构建它时,出现错误:
No property named "fieldindto" exists in source parameter(s). Did you mean "null"?
日志:
$ mvn clean package
...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project demo: Compilation failure
[ERROR] /path/src/main/java/com/example/demo/ExampleMapper.java:[9,48] No property named "fieldindto" exists in source parameter(s). Did you mean "null"?
$ java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10,mixed mode)
$ mvn -v
Apache Maven 3.5.0
Java version: 11.0.8,vendor: AdoptOpenJDK
Java home: /usr/lib/jvm/adoptopenjdk-11-hotspot-amd64
解决方法
我能够在本地计算机上重现完全相同的错误,这与以下事实有关:在生成lombok类之前,您将lombok与Mapstruct一起使用来尝试生成映射。
解决此问题的一种方法是在插件中,以确保lombok在mapstruct之前出现
Welcome
似乎<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.1.Final</version>
</path>
</annotationProcessorPaths>
考虑了路径的顺序。
注意:如果您使用的是最新的intellij,请不要忘记在更新订单后导入,否则您将收到相同的错误
,它适用于手写的吸气剂/设置器。
我从以下位置更改了龙目岛版本:
<lombok.version>1.18.16</lombok.version>
到大人:
<lombok.version>1.18.10</lombok.version>
它有效。
编辑:我已经在GitHub上发布了一个问题:https://github.com/mapstruct/mapstruct/issues/2271