[英]MapStruct - Nested mapping
I need to map a response from third party API to another response structure.我需要 map 第三方 API 对另一个响应结构的响应。 I'm using Mapstruct for that.我正在为此使用 Mapstruct。
ThirdParty API classes:第三方 API 类:
public class TPResponse {
protected UserListsType userLists;
protected ProductOffers offers;
//getters and setters
}
public class UserListsType {
protected UserTypes userTypes;
...............
}
public class UserTypes{
protected List<User> userType;
}
public class User{
protected String userID;
}
public class ProductOffers {
protected List<OffersType> OffersType;
}
public class OffersType{
protected PriceType totalPrice;
}
Our API classes:我们的 API 类:
public class ActualResponse {
private List<Customer> user = new ArrayList<Customer>();
//getters and setters
}
public class Customer{
private String customerId;
private String pdtPrice;
private OfferPrice offerPrice;
}
public class OfferPrice{
private String offerId;
}
I want to map these elements using MapStruct.我想使用 MapStruct 来 map 这些元素。
1) customerId <Map with> UserTypes->User->userID
2) pdtPrice <Map with> offers -> OffersType -> totalPrice
3) offerId <Map with> sum of (offers -> OffersType -> totalPrice)
I tried to write Mapper Class using MapStruct like:我尝试使用 MapStruct 编写 Mapper Class,例如:
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface UserResponseMapper {
UserResponseMapper RESPONSE_MAPPER = Mappers.getMapper(UserResponseMapper.class);
@Mappings({
@Mapping(target="customer.customerId", source="userTypes.userType.userId")
})
ActualResponse mapUser(UserListsType userListType);
}
Currently it shows the error like "Unknown property "customer.customerId" and "userTypes.userType.userId". Can anyone please help me to map all those elements using Mapstruct?目前它显示像“未知属性“customer.customerId”和“userTypes.userType.userId”这样的错误。谁能帮我map使用Mapstruct所有这些元素?
Question no 2: How can we map following?问题 2:我们如何才能关注 map? 1) customerId UserTypes->User->userID 2) pdtPrice offers -> OffersType -> totalPrice 3) offerId sum of (offers -> OffersType -> totalPrice) 1) customerId UserTypes->User->userID 2) pdtPrice offer -> OffersType -> totalPrice 3) offerId sum of (offers -> OffersType -> totalPrice)
I tried我试过了
@Mapping(target = "customerId", source = "userId")
@Mapping(target = "pdtPrice", source = "totalPrice")
User mapUser(UserListsType userListType, OffersType offersType );
I'm getting null values for customerId and pdtPrice我得到了 customerId 和 pdtPrice 的 null 值
From what I can understand you need to map the lists in ActualResponse
and UserTypeListType
.据我了解,您需要 map 中的列表ActualResponse
和UserTypeListType
。
When you provide a mapping between certain objects MapStruct can automatically generate mapping between its collections.当您提供某些对象之间的映射时,MapStruct 可以自动生成其 collections 之间的映射。 So in your case you'll need to do something like:因此,在您的情况下,您需要执行以下操作:
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface UserResponseMapper {
UserResponseMapper RESPONSE_MAPPER = Mappers.getMapper(UserResponseMapper.class);
@Mappings({
@Mapping(target="user", source="userType")
})
ActualResponse mapUser(UserTypeListType userListType);
@Mapping(target = "customerId", source = "userId")
User mapUser(UserType userType);
}
If you need to do calculations and have objects from different levels and you need to do some groupings and or aggregations I would suggest that you write your own logic.如果您需要进行计算并拥有来自不同级别的对象,并且需要进行一些分组和/或聚合,我建议您编写自己的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.