The example below displays the items from the emailAddress list in PeopleTO in the primaryEmailAddress and secondaryEmailAddress People properties.
MapStruct cannot be directly displayed in the collection, but it allows you to implement methods that run after matching to complete the process. I used one such method to map the primaryPhone and secondaryPhone PeopleTO to the elements of the phones list in People .
abstract class Mapper { @Mappings({ @Mapping(target="primaryEmailAddress", expression="emailAddress != null && emailAdress.size() >= 1 ? emailAdresses.get(0) : null"), @Mapping(target="secondaryEmailAddress", expression="emailAddress != null && emailAdress.size() >= 2 ? emailAdresses.get(1) : null"), @Mapping(target="phones", ignore=true) }) protected abstract People getPeople(PeopleTO to); @AfterMapping protected void setPhones(PeopleTO to, @MappingTarget People people) { people.setPhones(new List<String>()); people.getPhones().add(to.primaryPhone); people.getPhones().add(to.secondaryPhone); } }
source share