MapStruct: map nested object properties to properties

Now, with version 1.4 and above of mapstruct you can do this:

@Mapping(target = ".", source = "person")
PersonDTO personBLOToPersonDTO(PersonBLO personBLO);

It will try to map all the fields of person to the current target.


Using wildcards is currently not possible.

What you can do though is to provide a custom method that would just invoke the correct one. For example:

@Mapper
public interface MyMapper {

default PersonDTO personBLOToPersonDTO(PersonBLO personBLO) {
    if (personBLO == null) {
        return null;
    }
    PersonDTO dto = personToPersonDTO(personBlo.getPerson());
    // the rest of the mapping

    return dto;
}

PersonDTO personToPersonDTO(PersonBLO source);

}

Tags:

Java

Mapstruct