You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何关闭MapStruct生成Mapper方法时的全参数空校验提前返回逻辑

解决方案

你可以通过配置MapStruct的空值映射策略实现需求,具体实现方式如下:

前提要求

使用 MapStruct 1.4.2.Final及以上版本,该版本开始提供的NullValueMappingStrategy配置可直接控制多参数全空场景的返回逻辑。


方案1:单个Mapper单独配置

直接在你的@Mapper注解上添加nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT属性即可,修改后的Mapper代码如下:

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.NullValueMappingStrategy;

@Mapper(componentModel = "spring", nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface Mapper {

    @Mappings({
            @Mapping(target = "a", source = "sourceA.a"),
            @Mapping(target = "b", source = "sourceB.a"),
            @Mapping(target = "c", source = "sourceC.a")
    })
    Target toTarget(SourceA sourceA, SourceB sourceB, SourceC sourceC);
}

配置后MapStruct自动生成的实现代码会删除所有入参全空直接返回null的判断逻辑,无论入参是否全为null都会执行Builder构建流程,最终调用build()方法返回实例。


方案2:全局配置(所有Mapper生效)

如果需要所有Mapper都应用该规则,可以在编译参数中添加全局配置,无需每个Mapper单独加注解:

  • Maven配置(pom.xml中maven-compiler-plugin部分):
<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>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.5.5.Final</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.30</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>0.2.0</version>
            </path>
        </annotationProcessorPaths>
        <compilerArgs>
            <arg>-Amapstruct.nullValueMappingStrategy=RETURN_DEFAULT</arg>
        </compilerArgs>
    </configuration>
</plugin>
  • Gradle配置(build.gradle中compileJava部分):
compileJava {
    options.compilerArgs += [
        "-Amapstruct.nullValueMappingStrategy=RETURN_DEFAULT"
    ]
}

低版本MapStruct兼容方案

如果使用的MapStruct版本低于1.4.2.Final无法使用上述配置,可以在Mapper中自定义默认方法绕开自动生成的空校验逻辑:

@Mapper(componentModel = "spring")
public interface Mapper {

    @Mappings({
            @Mapping(target = "a", source = "sourceA.a"),
            @Mapping(target = "b", source = "sourceB.a"),
            @Mapping(target = "c", source = "sourceC.a")
    })
    Target internalToTarget(SourceA sourceA, SourceB sourceB, SourceC sourceC);

    default Target toTarget(SourceA sourceA, SourceB sourceB, SourceC sourceC) {
        Target.TargetBuilder builder = Target.builder();
        if (sourceA != null) builder.a(sourceA.getA());
        if (sourceB != null) builder.b(sourceB.getA());
        if (sourceC != null) builder.c(sourceC.getA());
        // 直接调用build(),由Builder自身的校验规则处理
        return builder.build();
    }
}

注意事项

如果需要Builder在构建时执行非空校验,只需要在Target类的对应字段上添加@NonNull注解,Lombok自动生成的Builder会在build()方法中自动做非空校验,不符合规则时直接抛出异常,符合你的需求。

内容的提问来源于stack exchange,提问作者user5706966

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 08:27:02