java 如何指导Mapstruct使用lombok builder?

9rbhqvlz  于 2022-12-21  发布在  Java
关注(0)|答案(3)|浏览(246)

当我尝试使用私有默认构造函数Map对象时,MapStruct无法创建实现,但使用了(lombok生成的)构建器。
SomeMapperImpl.java:[20,27] SomeDto() is not public in com.example.mapstructdemo.dto.SomeDto; cannot be accessed from outside package
日期:

@Value
@Builder
public class SomeDto {
}

型号:

@Value
@Builder
public class SomeModel {
}

Map器接口:

@Mapper
public interface SomeMapper {
    SomeDto map(SomeModel someModel);
    SomeModel map(SomeDto someDto);
}

来自Pom.xml的片段:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${compiler-plugin.version}</version>
        <configuration>
            <annotationProcessorPaths>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.16</version>
                </path>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-mapstruct-binding</artifactId>
                    <version>0.2.0</version>
                </path>
                <path>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>1.4.1.Final</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
    </plugin>

生成的实现:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-01-29T13:47:46+0100",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 11.0.9.1 (Ubuntu)"
)
public class SomeMapperImpl implements SomeMapper {

    @Override
    public SomeDto map(SomeModel someModel) {
        if ( someModel == null ) {
            return null;
        }

        SomeDto someDto = new SomeDto();

        return someDto;
    }

    @Override
    public SomeModel map(SomeDto someDto) {
        if ( someDto == null ) {
            return null;
        }

        SomeModel someModel = new SomeModel();

        return someModel;
    }
}

我可以做些什么来帮助mapstruct找到构建器?
要重现此问题,请克隆此repo https://github.com/rmvanderspek/mapstruct-demo并进行mvn验证。

wh6knrhe

wh6knrhe1#

我试过你的演示源代码,结果发现把Mapstruct放在编译器插件源代码的前面可以解决这个问题。我不知道为什么这样做,我觉得这是违反直觉的,但这是一个Mapstruct的开发人员在Lombok的Github上发布的类似问题中提出的建议。
所以在你的例子中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${compiler-plugin.version}</version>
    <configuration>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.mapstruct-processor.version}</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${org.projectlombok.lombok.version}</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>${org.projectlombok.ombok-mapstruct-binding.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
lb3vh1jj

lb3vh1jj2#

我把lombok工件放在依赖项中mapstruct和mapstruct-processor工件的下面,它就开始工作了。

<dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>${lombok-mapstruct-binding.version}</version>
        </dependency>
    </dependencies>
q5lcpyga

q5lcpyga3#

我花了几个小时才弄明白:
以防您没有使用Lombok @Builder为对象设置构建器模式。
确保生成器方法为publicstatic

public static SectionBuilder builder() {
        return new SectionBuilder();
 }

相关问题