我有一个带有嵌套对象列表的对象
@Getter
@Setter
@NoArgsConstructor
public class Notification {
private Long id
private Long statusId;
private List <External> external;
}
@Getter
@Setter
@NoArgsConstructor
public class External{
private Long externalId;
private LocalDate date;
}
Dto
@Getter
@Setter
@NoArgsConstructor
public class NotificationPayload {
private Long id;
private Long statusId;
private List <ExternalReferencePayload> external;
}
@Getter
@Setter
@NoArgsConstructor
public class ExternalReferencePayload {
private Long externalReferenceId;
}
映射器
@Mapper(componentModel = "spring")
public interface NotificationMapper{
public Notification dtoToNotification(NotificationPayload payload);
}
我搜索了映射嵌套列表的方式
答案 0 :(得分:0)
为了对某些元素执行自定义映射,只需要定义一个映射方法,MapStruct将负责其余的工作。在您的示例中:
@Mapper(componentModel = "spring")
public interface NotificationMapper{
public Notification dtoToNotification(NotificationPayload payload);
@Mapping(target = "externalId", source = "externalReferenceId")
public External dtoExternal(ExternalReferencePayload payload);
}
这样,嵌套列表将使用dtoExternal
映射方法执行映射。使用@Mapping
,您可以控制externalId
和externalReferenceId
之间的映射方式