简体   繁体   English

MapStruct @MappingTarget 生成一个空方法

[英]MapStruct @MappingTarget generates an empty method

Have a target type, formed by Lombok:有一个由 Lombok 形成的目标类型:

@Data
@Builder
class Target {
   private final String a;
}

and have a DTO:并有一个 DTO:

@Value
@Builder
class DTO {
   private final String a;
}

Mapper:映射器:

@Mapper(componentModel = "spring")
interface Mapper {
    void update(DTO dto, @MappingTarget Target target);
}

But when I compile (saw something related to JDK 11, and yes, it is 11 in my case), the compiled method is empty:但是当我编译时(看到一些与 JDK 11 相关的东西,是的,在我的例子中是 11),编译的方法是空的:

public void update(DTO source, Target target) {
   if (source == null) {
       return;
   }
}

And this is only relevant to MappingTarget.这仅与 MappingTarget 相关。 Using regular Mapping methods of 'createFromDTO' works correctly.使用“createFromDTO”的常规映射方法可以正常工作。

MapStruct 1.3.0地图结构 1.3.0

Maven only uses Mapstruct processor, then you need add in Maven plugin in your pom.xml, a configuration to Mapstruct works with Lombok processor. Maven 仅使用 Mapstruct 处理器,然后您需要在 pom.xml 中添加 Maven 处理器插件,这是 Mapstruct 与 Lombok 一起使用的配置。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <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>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>0.1.0</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

You don't need to include final modifier.您不需要包含 final 修饰符。 Use @Data instead @Value as previously mentioned.如前所述,使用@Data 而不是@Value。

In my tests, this is sufficient to Mapstruct 1.4.1.Final works with Lombok and JDK 11.在我的测试中,这足以 Mapstruct 1.4.1.Final 与 Lombok 和 JDK 11 一起使用。

Well, it turned to be Lombok + private final.好吧,它变成了龙目岛+私人决赛。

Mapper specifically wants Lombok's @Data to be stated instead of @Value (had thought that @Builder to be of use by MapStruct), and, as a result, needed to remove final from the fields, which were needed to be updated. Mapper 特别希望声明 Lombok 的@Data而不是@Value (原以为@Builder可以使用 @Builder),因此需要从需要更新的字段中删除final

Strange thing is, this was done by "google, error and try again" (plus manually cleaning the out directory from the project on every attempt, since MapStruct ignored these changes), while MapStruct never gave a warning (no flags for ignoring warnings or errors were declared).奇怪的是,这是由“谷歌,错误并重试”完成的(加上每次尝试时手动从项目中清除out目录,因为 MapStruct 忽略了这些更改),而 MapStruct 从未发出警告(没有忽略警告的标志或已声明错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM