MapStruct String to List Display

How do I match String for List and List to String?

We believe that we have the following classification

class People{ private String primaryEmailAddress; private String secondaryEmailAddress; private List<String> phones; //getter and setters } class PeopleTO{ private List<String> emailAddress; private String primaryPhone; private String secondaryPhone; //getter and setters } 

In Dozer and Orika we can easily match the next line of code

 fields("primaryEmailAddress", "emailAddress[0]") fields("secondaryEmailAddress", "emailAddress[1]") fields("phones[0]", "primaryPhone") fields("phones[1]", "secondaryPhone") 

How can I do the same display in MapStruct? Where can I find more examples on mapstruct?

+5
source share
2 answers

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); } } 
+6
source

Here you can see several examples: https://github.com/mapstruct/mapstruct-examples

Design this module for your specific requirement (Iterable to non-Iterable): https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-iterable-to-non-iterable

and one more here: http://blog.goyello.com/2015/09/08/dont-get-lost-take-the-map-dto-survival-code/

Not sure if you can match immutable with Iterable.

+1
source

All Articles