使用 mapstruct 映射嵌套对象
Mapping nested object with mapstruct
我创建如下映射。如何将平面 dto 对象属性(如街道、城市等)映射到域对象中的嵌套地址。当我尝试时出现错误:
[ERROR] diagnostic: Unknown property "address.postalCode" in return
type.
@Mapping(source = "city", target = "address.city"),
@Mapper(componentModel = "spring", uses = {})
public interface CompanyMapper {
@Mappings({
@Mapping(source = "id", target = "id"),
@Mapping(source = "street", target = "address.street"),
@Mapping(source = "city", target = "address.city"),
@Mapping(source = "postalCode", target = "address.postalCode"),
@Mapping(source = "province", target = "address.province"),
})
DomainObject map(DtoObject dto);
和类...
public class Address {
private String street;
private Integer streetNumber;
private String city;
private String postalCode;
private String province;
//getters and setters
}
public class DomainObject {
private String id;
private Address address;
//getters and setters
}
public class DtoObject {
private String id;
private String street;
private String city;
private String postalCode;
private String province;
//getters and setters
}
尚不支持在您尝试使用目标端时嵌套它。有一个对此的功能请求 (issue #389),但我们还没有开始实施它。
我找不到一种方法可以用一种方法做到这一点。这是我的解决方案:
@Mapper
public interface DtoObjectMapper {
Address toAddress(DtoObject dtoObject);
DomainObject toDomainObject(DtoObject dtoObject, Address address);
}
同时使用;
@Component
public class SomeClass {
@Autowired
private DtoObjectMapper dtoObjectMapper;
public DomainObject convert(DtoObject dtoObject) {
return dtoObjectMapper.toDomainObject(dtoObject, dtoObjectMapper.toAddress(dtoObject));
}
}
我创建如下映射。如何将平面 dto 对象属性(如街道、城市等)映射到域对象中的嵌套地址。当我尝试时出现错误:
[ERROR] diagnostic: Unknown property "address.postalCode" in return type. @Mapping(source = "city", target = "address.city"),
@Mapper(componentModel = "spring", uses = {})
public interface CompanyMapper {
@Mappings({
@Mapping(source = "id", target = "id"),
@Mapping(source = "street", target = "address.street"),
@Mapping(source = "city", target = "address.city"),
@Mapping(source = "postalCode", target = "address.postalCode"),
@Mapping(source = "province", target = "address.province"),
})
DomainObject map(DtoObject dto);
和类...
public class Address {
private String street;
private Integer streetNumber;
private String city;
private String postalCode;
private String province;
//getters and setters
}
public class DomainObject {
private String id;
private Address address;
//getters and setters
}
public class DtoObject {
private String id;
private String street;
private String city;
private String postalCode;
private String province;
//getters and setters
}
尚不支持在您尝试使用目标端时嵌套它。有一个对此的功能请求 (issue #389),但我们还没有开始实施它。
我找不到一种方法可以用一种方法做到这一点。这是我的解决方案:
@Mapper
public interface DtoObjectMapper {
Address toAddress(DtoObject dtoObject);
DomainObject toDomainObject(DtoObject dtoObject, Address address);
}
同时使用;
@Component
public class SomeClass {
@Autowired
private DtoObjectMapper dtoObjectMapper;
public DomainObject convert(DtoObject dtoObject) {
return dtoObjectMapper.toDomainObject(dtoObject, dtoObjectMapper.toAddress(dtoObject));
}
}