我有DTO和Domain对象。我正在使用Mapstruct将域对象复制到DTO对象。
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;
}
使用下面的Mapper将域映射到DTO。我不想将电话属性从域映射到DTO。怎么做?我尝试在映射中提供嵌套的目标属性,但它会出错:
public interface CompanyMapper {
//**below line gives error**
@Mapping(target = "address.telephone", ignore=true)
CompanyDTO map(AddressDTO dto);
}
答案 0 :(得分:4)
您当前的定义将地址映射到公司对象,这似乎不对。您需要声明两个方法,一个用于映射地址,另一个用于映射公司(其生成的实现将依次调用地址映射方法):
public interface CompanyMapper {
CompanyDTO map(Company company);
@Mapping(target="telephone", ignore=true)
AddressDTO map(Address address);
}