Mapstruct - mapping from multiple collections into a single collection

LJW :

Using MapStruct, I have two collections in my source object containing slightly different objects. I'd like to map the objects these two collections into a common type, and then insert them all into a single collection. Something like the below:

List<UserNumber> listsToDtoList(List<PhoneNumber> phoneNumbers, List<FaxNumber> faxNumbers);

UserNumber phoneToPhoneDto(PhoneNumber phone);
UserNumber faxToPhoneDto(FaxNumber fax);

What's the simplest way to do this with MapStruct?

Sjaak :

So I would make a mapping from my SourceObject (not write List<UserNumber> listsToDtoList(List<PhoneNumber> phoneNumbers, List<FaxNumber> faxNumbers);

So something along the lines:

@Mapping( target = "userNumbers", source = "phoneNumbers" )
Target map( Source source );

@AfterMapping // will be applied in the final part of the previous method
default void map ( Source source, @MappingTarget Target target ) {
 target.getUserNumbers().addAll(source.getFaxNumbers());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91159&siteId=1