I need to map two objects together using both custom and regular mapping. Some properties are explicit and don't need any modifications, just mapping (because they don't have the same name), but some others need transformations.
Let's say I have two objects :
public class A {
String id;
String name;
String firstname;
}
public class B {
String nameB;
String firstnameB;
}
And an interface :
@Mapper
public interface MyMapper {
@Mappings({
@Mapping(target="name", source="b.nameB"),
@Mapping(target="firstname", source="b.firstnameB"),
@Mapping(target="id", source="?", qualifiedByName="createId")
})
A BToA(B b);
@Named("createId")
default String createId(? ?) {
return nameB + firstnameB;
}
}
In this interface I want to map A.id to B.nameB + B.firstnameB in a custom mapping method. I looked into the mapstruct documentation but I cannot figure out how to get multiple sources or just the B object as a source.
Is this possible ? If not how can I achieve it ?