Issue
how can I map difference model list string to list model my code below;
my TaskList.class
@Getter
@Setter
private List<TaskGroupList> groupIds;
my TaskResponse.class
@Getter
@Setter
private List<String> groupIds;
My TaskGroupList class
@Getter
@Setter
private String ownerId;
My TaskListMapper.class
public abstract List<TaskResponse> toAllTaskListResponse(List<TaskList> taskList);
Solution
For mapping to a List, I think you need to use IterableMapping annotation. Refer below link -
https://mapstruct.org/documentation/stable/reference/html/#implicit-type-conversions
It has an example for list to list conversion
@Mapper
public interface CarMapper {
@IterableMapping(numberFormat = "$#.00")
List<String> prices(List<Integer> prices);
}
So, in your scenario, you could use the annotation. Also, as there is a List of List Objects, nested iterations are needed as below -
@IterableMapping(qualifiedBy = IterableMapping.class)
List<TaskResponse> toAllTaskListResponse(List<TaskList> taskList);
@IterableMapping(qualifiedByName = "taskGroup")
List<String> map(List<TaskGroupList> task);
@Named("taskGroup")
default String map(TaskGroupList t) {
return t.getOwnerId();
}
Answered By - SKumar