Java 使用mapstruct映射不同类型列表的元素

Java 使用mapstruct映射不同类型列表的元素,java,mapstruct,Java,Mapstruct,我们正在映射一个对象,该对象具有一个对象列表,所有对象都实现了父接口,但可能具有不同的实现。 但是,当我们映射列表时,似乎只映射了ParentClass中的值,而不是子类中的值。 但是直接映射子对象很好 public class ParentClass{ String name; int anotherParentField; List<ParentClass> relation; } public class ChildClass1 extends ParentClass{ St

我们正在映射一个对象,该对象具有一个对象列表,所有对象都实现了父接口,但可能具有不同的实现。 但是,当我们映射列表时,似乎只映射了ParentClass中的值,而不是子类中的值。 但是直接映射子对象很好

public class ParentClass{
String name;
int anotherParentField;
List<ParentClass> relation;
}

public class ChildClass1 extends ParentClass{
String customCLass1Field;
}

public class ChildClass2 extends ParentClass {
int intField;
}


public class ParentClassDto{
String name;
int anotherParentField;
List<ParentClassDto> relation;
}

public class ChildClass1Dto extends ParentClassDto{
String customCLass1Field;
}

public class ChildClass2Dto extends ParentClassDto {
int intField;
}
如果我们在一个列表中映射一个包含ChildClass2和ChildClass1的对象ChildClass1

要映射的对象: 对象ChildClass1的json格式如下:

{
   "name":"myName",
   "anotherParentField":"10",
   "customCLass1Field":"custom name",
   "relation":[
      {
         (This is of Object Type : ChildClass1)
         "name":"firstRelationName",
         "anotherParentField":"110",
         "customCLass1Field":"relationcustom name"
      },
      {
         (This is of Object Type : ChildClass2)
         "name":"secondRelationName",
         "anotherParentField":"110",
         "intField":"4"
      }
   ]
}
但当我们使用上面的映射器映射到dto时,我们得到:

{
   "name":"myName",
   "anotherParentField":"10",
   "customCLass1Field":"custom name",
   "relation":[
      {
         "name":"firstRelationName",
         "anotherParentField":"110",
      },
      {
         "name":"secondRelationName",
         "anotherParentField":"110",
      }
   ]
}
未映射子类中的任何字段。
缺少什么?

我认为除了使用自定义映射器之外,没有其他方法

以下是我的解决方案,他们称之为:

并在ParentClassMapper中添加@DecoratedWith注释:

@Mapper
@DecoratedWith(ParentClassMapperDecorator.class)
public interface ParentClassMapper {

  ParentClassDto convertToDto(ParentClass p);

  ParentClass convertDTOToModel(ParentClassDto dto);
}
对于儿童:

@Mapper(uses = {ParentClassMapper.class})
public interface ChildClass1Mapper{

  ChildClass1Dto convertToDto(ChildClass1 p);

  ChildClass1 convertDTOToModel(ChildClass1Dto dto);
}

@Mapper(uses = {ParentClassMapper.class})
public interface ChildClass2Mapper {

  ChildClass2Dto convertToDto(ChildClass2 p);

  ChildClass2 convertDTOToModel(ChildClass2Dto dto);
}
如果要测试:

@Test
  public void mapStruct_Inheritance_Test() throws Exception {

    ChildClass1Dto childClass1Dto = new ChildClass1Dto();
    childClass1Dto.name = "name1";
    childClass1Dto.anotherParentField = 1;
    childClass1Dto.customCLass1Field = "customCLass1Field1";

    List<ParentClassDto> parentClassDtos = new ArrayList<>();
    ChildClass1Dto childClass11Dto = new ChildClass1Dto();
    childClass11Dto.name = "name12";
    childClass11Dto.anotherParentField = 12;
    childClass11Dto.customCLass1Field = "customCLass1Field12";
    parentClassDtos.add(childClass11Dto);

    ChildClass2Dto childClass21Dto = new ChildClass2Dto();
    childClass21Dto.name = "name12";
    childClass21Dto.anotherParentField = 21;
    childClass21Dto.intField = 210;
    parentClassDtos.add(childClass21Dto);

    childClass1Dto.relation = parentClassDtos;

    ParentClass parentClass = Mappers.getMapper(ParentClassMapper.class).convertDTOToModel(childClass1Dto);
  }
@测试
public void mapStruct_继承_测试()引发异常{
ChildClass1Dto ChildClass1Dto=新的ChildClass1Dto();
childclassadto.name=“name1”;
ChildClassedTo.anotherParentField=1;
childClass1Dto.customCLass1Field=“customCLass1Field1”;
List parentClassDtos=new ArrayList();
ChildClass1Dto childClass11Dto=新的ChildClass1Dto();
childClass11Dto.name=“name12”;
ChildClass11To.anotherParentField=12;
childClass11Dto.customCLass1Field=“customCLass1Field12”;
parentClassDtos.add(childClass11Dto);
ChildClass2Dto childClass21Dto=新的ChildClass2Dto();
childClass21Dto.name=“name12”;
childClass21Dto.anotherParentField=21;
childClass21Dto.intField=210;
parentClassDtos.add(childClass21Dto);
childclassadto.relation=parentClassDtos;
ParentClass ParentClass=Mappers.getMapper(ParentClassMapper.class).convertdtomodel(childclassadto);
}
@Mapper(uses = {ParentClassMapper.class})
public interface ChildClass1Mapper{

  ChildClass1Dto convertToDto(ChildClass1 p);

  ChildClass1 convertDTOToModel(ChildClass1Dto dto);
}

@Mapper(uses = {ParentClassMapper.class})
public interface ChildClass2Mapper {

  ChildClass2Dto convertToDto(ChildClass2 p);

  ChildClass2 convertDTOToModel(ChildClass2Dto dto);
}
@Test
  public void mapStruct_Inheritance_Test() throws Exception {

    ChildClass1Dto childClass1Dto = new ChildClass1Dto();
    childClass1Dto.name = "name1";
    childClass1Dto.anotherParentField = 1;
    childClass1Dto.customCLass1Field = "customCLass1Field1";

    List<ParentClassDto> parentClassDtos = new ArrayList<>();
    ChildClass1Dto childClass11Dto = new ChildClass1Dto();
    childClass11Dto.name = "name12";
    childClass11Dto.anotherParentField = 12;
    childClass11Dto.customCLass1Field = "customCLass1Field12";
    parentClassDtos.add(childClass11Dto);

    ChildClass2Dto childClass21Dto = new ChildClass2Dto();
    childClass21Dto.name = "name12";
    childClass21Dto.anotherParentField = 21;
    childClass21Dto.intField = 210;
    parentClassDtos.add(childClass21Dto);

    childClass1Dto.relation = parentClassDtos;

    ParentClass parentClass = Mappers.getMapper(ParentClassMapper.class).convertDTOToModel(childClass1Dto);
  }