Mapstruct使用時報Unknown property xxx in result type xxx. Did you mean null?


0.背景

使用mapstruct時出現:

Unknown property "xxx" in result type xxx. Did you mean "null"?

同時,項目中也用到了lombok。

mapstruct版本: 1.4.1.Final

lombok版本: 1.8.22 (繼承的Spring2.5.6)

<!-- mapstruct START-->
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>${mapstruct.version}</version>
    <!--這個scope 只能作用在編譯和測試時,同時沒有傳遞性。-->
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>${mapstruct.version}</version>
    <scope>provided</scope>
</dependency>
<!-- mapstruct END -->

<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>

1.解決

1.1 方案1:調整順序

代碼生成器annotationProcessor標簽部分,將lombok放在mapstruct之前

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!--自動生成代碼annotationProcessor-->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

1.2 方案2:官網嘰嘰歪歪一堆

官網說Lombok 1.18.16或更高版本要添加lombok-mapstruct-binding的依賴,但是經過方案1我的問題它就解決了

原文在這里:https://mapstruct.org/faq/

其實文中也提到了先在模塊1使用lombok再在模塊2使用mapstruct。

image-20220121002958457

留個備用:

        <!-- mapStruct-lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
            <scope>provided</scope>
        </dependency>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM