Mapping List to another and modifying fields [Mapstruct]

Viewed 460

I'm trying to use mapstruct to map one list to another .While mapping i want to add some info in one particular field but unable to do it . I will be glad if someone could able to provide some solution.

ex- If i've  List of People( {"1", "John"}, {"2", "Tom"} ) 
modify it to ->  Employee({"1","Employee with name: John"}, {"2","Employee with name: Tom"} )

public class Employee {
String id;
String info;
}   

public class People {
String id;
String name;
}    


@Mapping(source = "people.name", target = "displayName", qualifiedByName = "displayName")
List<Employee> toEmployee(List<People> people);


@Named("displayName")
default String getAudioId(String name){
    return "Employee with name:" + name;
}
1 Answers

You need to place the mapping annotation on the Employee to People mapping method instead of the list mapping method.

For example:

List<Employee> toEmployee(List<People> people);

@Mapping(source = "name", target = "info", qualifiedByName = "displayName")
Employee toEmployee(People people);

mapstruct will use the toEmployee(People) mapping method inside the toEmployee(List<People>) mapping method.

Related