英文:
MapStruct - Nested mapping
问题
I need to map a response from third party API to another response structure. I'm using Mapstruct for that.
ThirdParty API classes:
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:
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.
- customerId
 - pdtPrice
 - offerId
 
I tried to write Mapper Class using MapStruct like:
@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?
Question no 2: How can we map the following?
- customerId
 - pdtPrice
 - offerId
 
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.
英文:
I need to map a response from third party API to another response structure. I'm using Mapstruct for that.
ThirdParty API classes :
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 :
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.
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 :
@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?
Question no 2 : How can we map following?
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
@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
答案1
得分: 2
从我理解的内容来看,您需要对ActualResponse和UserTypeListType中的列表进行映射。
当您为特定对象提供映射时,MapStruct可以自动生成其集合之间的映射。所以在您的情况下,您需要做类似以下的操作:
@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);
}
如果您需要进行计算,并且需要来自不同层次的对象,以及需要进行一些分组或聚合操作,我建议您编写自己的逻辑。
英文:
From what I can understand you need to map the lists in ActualResponse and UserTypeListType.
When you provide a mapping between certain objects MapStruct can automatically generate mapping between its 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论