我目前正在研究Kotlin上的mapstruct映射,它有一些关系,使用Spring引导存储库和服务来获取对象以供进一步处理。但是,我无法实现@AfterMapping。
@Mapper(componentModel = "spring")
interface Objectmapper {
@Mappings(
Mapping(source = "aCode", target = "a.code"),
Mapping(source = "bCode", target = "b.code")
)
fun convertFormDtoToEntity(
dto: ObjectFormDto,
@Context aRepo: ARepository,
@Context bService: BService
): Object
@AfterMapping
fun afterMappingFormDtoToEntity(
dto: ObjectFormDto,
@Context aRepo: ARepository,
@Context bService: BService,
@MappingTarget object: Object
){
object.a = aRepo.findByA(object.a.code)
object.b = bService.getB(object.b.code)
}
}我的目标是在完成对afterMappingFormDtoToEntity()的映射之后实现convertFormToEntity(),但是我无法完成kaptKotlin任务并返回错误
error: @AfterMapping can only be applied to an implemented class
public abstract void afterMappingFormDtoToEntity(@org.jetbrains.annotations.NotNull()...我当前的mapstruct版本是"1.5.2.Final",kapt版本为"1.6.10",kapt设置为build.gradle.kts
kapt {
arguments {
arg("mapstruct.unmappedTargetPolicy", "IGNORE")
}
keepJavacAnnotationProcessors = true
}发布于 2022-08-12 10:03:05
它是通过改变映射器类型来解决的。
从interface Objectmapper到abstract class ObjectMapper并将所有方法转换为抽象( @AfterMapping方法除外)
细节是写在我的媒体博客文章:https://medium.com/me/stats/post/5c3f468ee261
发布于 2022-08-12 13:21:07
Kotlin接口中的已实现方法未标记为已实现。因此,MapStruct认为该方法是抽象的,需要实现。
为了工作,您需要用@JvmDefault对方法进行注释
https://stackoverflow.com/questions/73332031
复制相似问题