无法使用MapStruct映射嵌套的@Data



当尝试使用@Data和@Builder映射嵌套对象时,mapStruct抛出以下错误:"找不到属性"的读取访问器;配置文件";在目标类型中">

@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);
}

使用龙目简化模型以简化

@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;
}
}

两个第一个映射按预期工作,但当试图将两者结合时,在编译时会抛出以下错误";找不到属性"的读取访问器;配置文件";在目标类型中">

如果有人能帮我做这件事,我将不胜感激。

非常感谢

乔纳森。

解决方案:

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

我们可以添加另一个映射器,比如ProfileMapper,然后在"父映射器"(例如UserMapper(中使用类似的映射器注释

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

相关内容

  • 没有找到相关文章

最新更新