使用MapStruct将实体映射到带有DI的DTO

Ahm*_*ziz 3 spring mapstruct

我是maptruct中的新手,我使用spring作为DI我跟进了关于DI容器的MapStruct文档页面4.2我尝试将我的实体映射到dto,如下所示:

@Mapper(componentModel = "spring")
public interface CustomerMapper {
@Mapping(source = "registered",target = "activeProfile")
CustomerDto customerToCustomerDto(Customer customer);
}
Run Code Online (Sandbox Code Playgroud)

当我运行mvn install时出现此错误:

java:27: error: No property named "registered" exists in source parameter(s).
@Mapping(source = "registered",target = "activeProfile")
Run Code Online (Sandbox Code Playgroud)

我的实体使用lombok,我相信有注册的字段

请帮忙

Pav*_*ely 5

您不必删除Lombok.你可以设置它在MapStruct之前工作,如ahus1 https://github.com/mapstruct/mapstruct/issues/510所述

<!-- first de-lombok the sources to make getters/setters visible for mapstruct,
but *DON'T'* add the output directory to the sources, as we will only need it for mapstruct processing -->
<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>${org.projectlombok.maven.version}</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
            <configuration>
                <sourceDirectory>src/main/java</sourceDirectory>
                <addOutputDirectory>false</addOutputDirectory>
                <outputDirectory>${project.build.directory}/delombok</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

<!-- use the de-lomobok files to create the necessary mappers -->
<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <configuration>
        <defaultOutputDirectory>
            ${project.build.directory}/generated-sources/mapstruct
        </defaultOutputDirectory>
        <processors>
            <processor>org.mapstruct.ap.MappingProcessor</processor>
        </processors>
        <sourceDirectory>
            ${project.build.directory}/delombok
        </sourceDirectory>
    </configuration>
    <executions>
        <execution>
            <id>process</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>process</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- now take the original sources together with the created mappers to the compiler -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <annotationProcessors>
            <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)


Ahm*_*ziz 2

lombok从实体中删除并手动创建了 setter /getters 并且运行良好