MapStruct @MappingTarget 生成一个空方法

有一个由 Lombok 形成的目标类型:


@Data

@Builder

class Target {

   private final String a;

}

并有一个 DTO:


@Value

@Builder

class DTO {

   private final String a;

}

映射器:


@Mapper(componentModel = "spring")

interface Mapper {

    void update(DTO dto, @MappingTarget Target target);

}

但是当我编译时(看到一些与 JDK 11 相关的东西,是的,在我的例子中是 11),编译后的方法是空的:


public void update(DTO source, Target target) {

   if (source == null) {

       return;

   }

}

并且这只与MappingTarget相关。使用“createFromDTO”的常规映射方法可以正常工作。


地图结构1.3.0


一只斗牛犬
浏览 205回答 2
2回答

开满天机

Maven只使用Mapstruct处理器,那么你需要在pom.xml中添加Maven插件,配置Mapstruct与Lombok处理器一起使用。<plugin>&nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; <version>3.8.1</version>&nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; <annotationProcessorPaths>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.mapstruct</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>mapstruct-processor</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${org.mapstruct.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.projectlombok</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>lombok</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${org.projectlombok.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.projectlombok</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>lombok-mapstruct-binding</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>0.1.0</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </path>&nbsp; &nbsp; &nbsp; &nbsp; </annotationProcessorPaths>&nbsp; &nbsp; </configuration></plugin>您不需要包含 Final 修饰符。如前所述,使用 @Data 而不是 @Value。在我的测试中,这足以让 Mapstruct 1.4.1.Final 与 Lombok 和 JDK 11 配合使用。

慕标5832272

好吧,结果变成了龙目岛+私人决赛。Mapper 特别希望 Lombok@Data被声明@Value(而不是@Builder被 MapStruct 使用),因此,需要final从需要更新的字段中删除。奇怪的是,这是通过“google,错误并重试”完成的(加上out每次尝试时都从项目中手动清除目录,因为 MapStruct 忽略了这些更改),而 MapStruct 从未给出警告(没有忽略警告或错误的标志)被宣布)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java