【问题标题】:Cannot map nested @Data with MapStruct无法使用 MapStruct 映射嵌套的 @Data
【发布时间】:2020-10-09 21:37:30
【问题描述】:

当尝试使用@Data 和@Builder 映射嵌套对象时,mapStruct 会抛出以下错误:“No read accessor found for property “profile” in target type。”

@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 简化模型

@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class RequestCreateOktaUser {

  private Profile profile;

  @Data
  @NoArgsConstructor
  @AllArgsConstructor(access = AccessLevel.PRIVATE)
  @Builder
  public static class Profile {
    private String login;
    private String email;
    private Integer organization;
  }

}
@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class RequestMobilePreRegisterUser {

  @Valid
  private User user;


  @Data
  @NoArgsConstructor
  @AllArgsConstructor(access = AccessLevel.PRIVATE)
  @Builder
  public static class User {

    @Valid
    private Profile profile;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor(access = AccessLevel.PRIVATE)
    @Builder
    public static class Profile {

      @NotEmpty
      @Email
      private String email;

    }

}

前两个映射按预期工作,但在尝试将两者结合时,在编译时抛出以下错误“目标类型中的属性“配置文件”未找到读取访问器。”

如果有人能在这方面帮助我,我将不胜感激。

非常感谢,

乔纳森。

【问题讨论】:

    标签: spring-boot lombok mapstruct


    【解决方案1】:

    解决方案:

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

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 2023-03-29
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2019-07-13
      • 1970-01-01
      相关资源
      最近更新 更多