我有以下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;
}
使用下面的映射器将域映射到DTO.我不想将电话属性从域映射到DTO.怎么做?我试图在映射忽略中提供嵌套的目标属性,但它给出了错误:
public interface CompanyMapper {
//**below line gives error**
@Mapping(target = "address.telephone", ignore=true)
CompanyDTO map(AddressDTO dto);
}
解决方法:
您当前的定义将地址映射到看起来不正确的公司对象中.您需要声明两种方法,一种用于映射地址,一种用于映射公司(其生成的实现将依次调用地址映射方法):
public interface CompanyMapper {
CompanyDTO map(Company company);
@Mapping(target="telephone", ignore=true)
AddressDTO map(Address address);
}