MapStruct – 嵌套映射

huangapple go评论92阅读模式
英文:

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.

  1. customerId UserTypes->User->userID
  2. pdtPrice offers -> OffersType -> totalPrice
  3. offerId 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 the following?

  1. customerId UserTypes->User->userID
  2. pdtPrice offers -> 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.

英文:

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&lt;User&gt; userType;
}

public class User{
 protected String userID;
}

public class ProductOffers {
    protected List&lt;OffersType&gt; OffersType;
}

public class OffersType{
   protected PriceType totalPrice;
}

Our API classes :

public class ActualResponse {
       private List&lt;Customer&gt; user = new ArrayList&lt;Customer&gt;();
       //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 &lt;Map with&gt; UserTypes-&gt;User-&gt;userID
2) pdtPrice  &lt;Map with&gt;  offers -&gt; OffersType -&gt; totalPrice
3) offerId  &lt;Map with&gt; sum of (offers -&gt; OffersType -&gt; 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=&quot;customer.customerId&quot;, source=&quot;userTypes.userType.userId&quot;)
    })
    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 = &quot;customerId&quot;, source = &quot;userId&quot;)
@Mapping(target = &quot;pdtPrice&quot;, source = &quot;totalPrice&quot;)
    User mapUser(UserListsType userListType, OffersType offersType );

I'm getting null values for customerId and pdtPrice

答案1

得分: 2

从我理解的内容来看,您需要对ActualResponseUserTypeListType中的列表进行映射。

当您为特定对象提供映射时,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=&quot;user&quot;, source=&quot;userType&quot;)
    })
    ActualResponse mapUser(UserTypeListType userListType);

    @Mapping(target = &quot;customerId&quot;, source = &quot;userId&quot;)
    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.

huangapple
  • 本文由 发表于 2020年7月23日 01:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63040246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定