Search code examples
javamapperorika

Orika mapper - map to List<String>


I have one object with the field List<AnotherObject> and I want to map it to a second object with field List<String>. I need to map 2 objects. I can not find the way.

Situation map class One -> class Two :

public class One {
    String field11;
    List<AnotherObject> field12;
}

public class AnotherObject {
    String field31;
    String field32;
}

public class Two {
    String field21;
    List<String> field22;
}

mapperFactory.classMap(One.class, Two.class)
    .fieldAToB("field11", "field21") //ok
    .fieldAToB("field12{field31}", "field22") //KO //because this is String (end element) --- > List<String>

The real business is much bigger than the example so I rule out making a custom mapper.


Solution

  • You have to map it like this:

    mapperFactory.classMap(One.class, Two.class)
        .fieldAToB("field11", "field21") //ok
        .fieldAToB("field12{field31}", "field22{}") //ok
    

    Notice the empty braces {}