Spring Boot 无法使用MapStructMap嵌套的@Data

qv7cva1a  于 2023-03-18  发布在  Spring
关注(0)|答案(2)|浏览(268)

尝试使用@Data和@BuilderMap嵌套对象时,mapStruct抛出以下错误:未找到目标类型中属性“profile”的读访问器。

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

  // only for testing mapping is working
  @Mapping(target = "profile.organization", source = "organization")
  RequestCreateOktaUser toEntity(Integer organization);

  // only for testing mapping is working
  @Mapping(target = "profile.login", source = "request.profile.email")
  RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request);
  
  // Throw error "No read accessor found for property "profile" in target type" at compile time
  @Mapping(target = "profile.organization", source = "organization")
  @Mapping(target = "profile.login", source = "request.profile.email")
  RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request, Integer organization);

}

使用Lombok简化模型以简化
x一个一个一个一个x一个一个二个x
前两个Map按预期工作,但在尝试合并这两个Map时,在编译时引发以下错误:“未找到目标类型中属性“profile”的读访问器。”
如果有人能在这件事上帮助我,我将非常感激。
多谢了
乔纳森。

ddarikpa

ddarikpa1#

解决方案:

@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
sd2nnvve

sd2nnvve2#

我们可以添加另一个Map器,比如ProfileMapper,而不是忽略构建器,然后在“父Map器”(例如UserMapper)中,您可以像这样使用Map器注解

@Mapper(uses = {ProfileMapper.class})
interface UserMapper {
...
}

相关问题