Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 映射嵌套元素-Mapstruct_Java_Spring Boot_Nested Lists_Mapstruct_Object Object Mapping - Fatal编程技术网

Java 映射嵌套元素-Mapstruct

Java 映射嵌套元素-Mapstruct,java,spring-boot,nested-lists,mapstruct,object-object-mapping,Java,Spring Boot,Nested Lists,Mapstruct,Object Object Mapping,我正在尝试使用MapStruct将以下源类映射到目标类 目标类别: public class Response { private List<Customer> customer = new ArrayList<Customer>(); } public class Customer { private String customerId; private List<Product> products = new ArrayList&

我正在尝试使用MapStruct将以下源类映射到目标类

目标类别:

public class Response {
    private List<Customer> customer = new ArrayList<Customer>();
}

public class Customer {
    private String customerId;
    private List<Product> products = new ArrayList<Product>();
}

public class CustProduct {
    private String CustProductId;
    private String CustPdtName;
    private List<productDetail> CustProductDetails = new ArrayList<productDetail>();
}
public class UserList {
    protected List<User> user;
}

public class User {
    protected String userId;
    protected List<String> productRefId;  //List of products for that particular user
}

public class ProductList {
    protected List<Product> product;
}
public class Product {
   protected String productId;       //Reference to productRefId
   protected String productName;
   protected List<Details> productDetails;
}
   
公共类响应{
private List customer=new ArrayList();
}
公共类客户{
私有字符串customerId;
私有列表产品=新的ArrayList();
}
公共类产品{
私有字符串CustProductId;
私有字符串CustPdtName;
private List CustProductDetails=new ArrayList();
}
源类:

public class Response {
    private List<Customer> customer = new ArrayList<Customer>();
}

public class Customer {
    private String customerId;
    private List<Product> products = new ArrayList<Product>();
}

public class CustProduct {
    private String CustProductId;
    private String CustPdtName;
    private List<productDetail> CustProductDetails = new ArrayList<productDetail>();
}
public class UserList {
    protected List<User> user;
}

public class User {
    protected String userId;
    protected List<String> productRefId;  //List of products for that particular user
}

public class ProductList {
    protected List<Product> product;
}
public class Product {
   protected String productId;       //Reference to productRefId
   protected String productName;
   protected List<Details> productDetails;
}
   
公共类用户列表{
受保护的列表用户;
}
公共类用户{
受保护的字符串用户标识;
protected List productRefId;//该特定用户的产品列表
}
公共类产品列表{
受保护产品清单;
}
公共类产品{
受保护的字符串productId;//对productRefId的引用
受保护的字符串名称;
受保护产品清单详情;
}
映射器接口:

 List<Customer> mapUser(List<User> user);

    @Mappings({
            @Mapping(target = "customerId", source = "userId”),
            @Mapping(target = "products", ignore = true)
    })
    Customer mapUser(User user);

    @Mappings({
        @Mapping(target = "CustProductId", source = "productId"),
        @Mapping(target = "CustPdtName", source = "productName"),
        @Mapping(target = "CustProductDetails", source = "productDetails")
})
CustProduct mapUser(Product product);
列表映射用户(列表用户);
@映射({
@映射(target=“customerId”,source=“userId”),
@映射(target=“products”,ignore=true)
})
客户映射用户(用户);
@映射({
@映射(target=“CustProductId”,source=“productId”),
@映射(target=“CustPdtName”,source=“productName”),
@映射(target=“CustProductDetails”,source=“productDetails”)
})
客户产品映射用户(产品);
我的问题是,我想将客户产品客户 为此,我尝试了如下方法:

default void findProducts(User user, @MappingTarget Customer customer) {
            List<String> productIds = user.getproductRefId();
            List<CustProduct> custProducts = new ArrayList<>();
            for(int i=0; i<productIds.size();i++){
                    CustProduct custProduct = new CustProduct();
                    custProduct.setCustProductId(productIds.get(i));
                    //Here I want set productName and productDetails to custProduct Object(Iterating through ProductList and get from Product)
                    custProducts.add(custProduct);
                }
            }
            customer.setCustProducts(custProducts);
        }
    
默认无效findProducts(用户用户,@MappingTarget客户){
List productIds=user.getproductRefId();
List custProducts=new ArrayList();
对于(inti=0;i您需要使用注释将ProductList对象带入上下文

调用
mapUser
时,将映射器方法更改为下面的定义并传递ProductList对象:

@Mappings({
            @Mapping(target = "customerId", source = "paxJourneyType.paxJourneyID”),
            @Mapping(target = "products", ignore = true)
    })
    Customer mapUser(User user, @Context ProductList productList);
然后您可以在
@AfterMapping
方法中使用相同的ProductList对象:

default void findProducts(User user, @Context ProductList productList @MappingTarget Customer customer) {
            List<String> productIds = user.getproductRefId();
            List<CustProduct> custProducts = new ArrayList<>();
            for(int i=0; i<productIds.size();i++){
                    CustProduct custProduct = new CustProduct();
                    custProduct.setCustProductId(productIds.get(i));
                    Product product = getProduct(ProductList productList,productIds.get(i));
                    custProduct.setCustPdtName(product.getProductName);
                    custProducts.add(custProduct);
                }
            }
            customer.setCustProducts(custProducts);
        }

private Product getProduct(ProductList productList,String productId){
    //Iterate through ProductList and get from Product
}
default void find产品(用户用户@Context ProductList ProductList@MappingTarget Customer){
List productIds=user.getproductRefId();
List custProducts=new ArrayList();

对于(int i=0;i您的@AfterMapping方法不起作用,因为@MappingTarget应该是生成器类型

@AfterMapping
default void findProducts(User user, @MappingTarget Customer.CustomerBuilder customer) {
...
}

您可以在不使用
@AfterMapping
的情况下执行此操作,但需要稍微帮助MapStruct:

@Mapper
公共接口映射器{
@映射(target=“customerId”,source=“userId”)
@映射(target=“products”,source=“productRefIds”)
客户映射(用户用户,@Context-map-productsMap);
列表映射(列表productRefIds,@Context-map-productsMap);
默认CustProduct映射(字符串productId,@Context map productsMap){
返回映射(productsMap.get(productId));
}
@映射(target=“custProductId”,source=“productId”)
@映射(target=“custProductName”,source=“productName”)
@映射(target=“custProductDetails”,source=“productDetails”)
客户产品图(产品);
CustProductDetail地图(ProductDetail ProductDetail);
}
或者,您可以手动迭代
productRefIds

@Mapper
public interface CustMapper {

    @Mapping(target = "customerId", source = "userId")
    @Mapping(target = "products", source = "productRefIds")
    Customer map(User user, @Context Map<String, Product> productsMap);

    default List<CustProduct> map(List<String> productRefIds, @Context Map<String, Product> productsMap) {
        return productRefIds.stream().map(productsMap::get).map(this::map).collect(Collectors.toList());
    }

    @Mapping(target = "custProductId", source = "productId")
    @Mapping(target = "custProductName", source = "productName")
    @Mapping(target = "custProductDetails", source = "productDetails")
    CustProduct map(Product product);

    CustProductDetail map(ProductDetail productDetail);
}
@Mapper
公共接口映射器{
@映射(target=“customerId”,source=“userId”)
@映射(target=“products”,source=“productRefIds”)
客户映射(用户用户,@Context-map-productsMap);
默认列表映射(列表productRefIds,@Context-map-productsMap){
返回productRefIds.stream().map(productsMap::get).map(this::map).collect(Collectors.toList());
}
@映射(target=“custProductId”,source=“productId”)
@映射(target=“custProductName”,source=“productName”)
@映射(target=“custProductDetails”,source=“productDetails”)
客户产品图(产品);
CustProductDetail地图(ProductDetail ProductDetail);
}
在这两种情况下,当
productsMap
中不存在
productId
时,您需要以某种方式处理这种情况


不使用
@AfterMapping
的优点是目标类可以是不可变的。

我尝试了这个。它映射“Customer mapUser(User User User)”但“List mapUser(List User)”->这个映射现在不起作用。也就是说,我变得像这样->“User”:[{“productDetails”:[]},{“productDetails”:[]},],实现已从:1.公共列表映射用户(列表用户){…….列表列表=新的ArrayList(users.size());for(User-User:users){List.add(mapUser(User));}以上实现已更改为for(User-User:users){List.add(userToCustomer(user));}和userToCustomer方法没有userNo的任何设置器…您必须在mapUser方法中使用pass ProductList,以便它进入上下文。请尝试以下操作:在列表中使用mapUser(user,ProductList)。如果我们像Customer mapUser(user user user user,@context ProductList ProductList)那样修改,请添加(),然后是列表映射用户(列表用户);将不起作用。