gpt4 book ai didi

java - 如何指示 Mapstruct 使用 lombok builder?

转载 作者:行者123 更新时间:2023-12-04 00:51:39 27 4
gpt4 key购买 nike

当我尝试使用私有(private)默认构造函数但使用(lombok 生成的)构建器映射对象时,MapStruct 无法创建实现。

SomeMapperImpl.java:[20,27] SomeDto() is not public incom.example.mapstructdemo.dto.SomeDto; cannot be accessed from outsidepackage


Dto:
@Value
@Builder
public class SomeDto {
}
模型:
@Value
@Builder
public class SomeModel {
}
映射器接口(interface):
@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 验证。

最佳答案

我已经玩过您的演示源,结果证明将 Mapstruct 首先放在编译器插件源中可以解决问题。我不知道为什么会这样,这对我来说是违反直觉的,但这是 Mapstruct 的开发者之一在 a similar issue posted on Lombok's 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>

关于java - 如何指示 Mapstruct 使用 lombok builder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65955000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com