2
votes

I have following DTO and Domain objects. I am using Mapstruct to copy domain object to DTO object.

public class AddressDomain {
            private String street;
            private Telephone telephone;
    }
public class CompanyDomain{
        private String id;
        private Address address;
}

public class AddressDTO {
            private String street;
            private Telephone telephone;
    }
public class CompanyDTO{
        private String id;
        private Address address;
}

Mapping Domain to DTO using below Mapper. i don't want to map telephone property from domain to DTO. How to do that? i tried providing nested target property in mapping ignore but it gives error:

public interface CompanyMapper {
    //**below line gives error**
    @Mapping(target = "address.telephone", ignore=true)
    CompanyDTO map(AddressDTO dto);
}
1

1 Answers

4
votes

Your current definition maps an address into a company object which doesn't seem right. You need to declare two methods, one for mapping addresses and one for mapping companies (whose generated implementation will in turn invoke the address mapping method):

public interface CompanyMapper {

    CompanyDTO map(Company company);

    @Mapping(target="telephone", ignore=true)
    AddressDTO map(Address address);
}