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

使用MapStruct+protobuf-spi-impl映射Proto类到Lombok DTO编译报错

错误原因
  • 核心问题是你用的映射框架(大概率是MapStruct,你提到的BaseDataMapper应该是它的注解接口)默认没有内置对com.google.type.Date(Protobuf定义的日期类型)的转换逻辑,编译时找不到该类型与DTO日期字段的适配转换器,导致无法生成Mapper实现类。
  • 若同时搭配Lombok,可能存在注解处理器执行顺序问题,但这是次要因素,核心矛盾还是com.google.type.Date的转换缺失。
解决办法

1. 自定义类型转换器

手动实现com.google.type.Date与目标日期类型的互转逻辑,再在Mapper接口中引入:

步骤1:编写转换器类

import com.google.type.Date;
import java.time.LocalDate;

public class GoogleDateConverter {
    // Protobuf Date → LocalDate
    public static LocalDate googleToLocal(Date googleDate) {
        if (googleDate == null) return null;
        return LocalDate.of(googleDate.getYear(), googleDate.getMonth(), googleDate.getDay());
    }

    // LocalDate → Protobuf Date
    public static Date localToGoogle(LocalDate localDate) {
        if (localDate == null) return null;
        return Date.newBuilder()
                .setYear(localDate.getYear())
                .setMonth(localDate.getMonthValue())
                .setDay(localDate.getDayOfMonth())
                .build();
    }
}

如果DTO用的是java.util.Date,则修改转换器适配该类型:

import com.google.type.Date;
import java.util.Date;
import java.time.LocalDate;
import java.time.ZoneId;

public class GoogleDateConverter {
    public static Date googleToUtil(Date googleDate) {
        if (googleDate == null) return null;
        LocalDate localDate = LocalDate.of(googleDate.getYear(), googleDate.getMonth(), googleDate.getDay());
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date utilToGoogle(Date utilDate) {
        if (utilDate == null) return null;
        LocalDate localDate = utilDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return Date.newBuilder()
                .setYear(localDate.getYear())
                .setMonth(localDate.getMonthValue())
                .setDay(localDate.getDayOfMonth())
                .build();
    }
}

步骤2:在Mapper接口中引入转换器

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper(uses = GoogleDateConverter.class) // 关联自定义转换器
public interface BaseDataMapper {
    BaseDataMapper INSTANCE = Mappers.getMapper(BaseDataMapper.class);

    // 你的映射方法,例如:
    YourDto protoToDto(YourProtobufEntity protoEntity);
}

2. 修正Maven注解处理器顺序(Lombok+MapStruct场景)

确保Lombok先处理生成getter/setter,再让MapStruct识别字段,在pom.xml中调整:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source> <!-- 匹配你的JDK版本 -->
                <target>11</target>
                <annotationProcessorPaths>
                    <!-- 先执行Lombok处理器 -->
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <!-- 再执行MapStruct处理器 -->
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                    <!-- 可选:添加Lombok与MapStruct的绑定依赖 -->
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>${lombok-mapstruct-binding.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 16:15:42