【发布时间】: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