spring – 使用MapStruct将实体映射到DTO

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

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

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

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

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

请帮忙

解决方法:

您不必删除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>
上一篇:java – 使用MapStruct进行转换时防止循环引用


下一篇:java-Spring Boot Mapstruct *错误