【问题标题】:How to handle conversion of a Set to a List with ModelMapper如何使用 ModelMapper 处理 Set 到 List 的转换
【发布时间】:2017-05-31 10:47:14
【问题描述】:

我有以下课程:

public class MyEntity {
  private Set<MyOtherEntity> other;
}

public class MyDTO {
  private List<MyOtherDTO> other;
}

我创建了两个PropertyMaps(使用ModelMapper),一个用于从DTO 到DTO 的每次转换

public class DTOToEntityPropertyMap extends PropertyMap<MyDTO, MyEntity> {
  @Override
  protected void configure() {
    List<MyOtherDTO> myOtherDTOs = source.getOther();
    Set<MyOtherEntity> myOtherEntities = new HashSet<>();
    for (MyOtherDTO myOtherDTO : myOtherDTOs) {
      MyOtherEntity myOtherEntity = ModelMapperConverterService.convert(myOtherDTO, MyOtherEntity.class);
      myOtherEntities.add(myOtherEntity);
    }
    map().setOther(myOtherEntities);
  }
}

public class EntityToDTOPropertyMap extends PropertyMap<MyEntity, MyDTO> {
  @Override
  protected void configure() {
    Set<MyOtherEntity> myOtherEntities = source.getOther();
    List<MyOtherDTO> myOtherDTOs = new ArrayList<>();
    for (MyOtherEntity myOtherEntity : myOtherEntities) {
      MyOtherDTO myOtherDTO = ModelMapperConverterService.convert(myOtherEntity, MyOtherDTO.class);
      myOtherDTOs.add(myOtherDTO);
    }
    map().setOther(myOtherDTOs);
  }
}

PropertyMaps 添加到ModelMapper 会产生以下错误:

引起:org.modelmapper.ConfigurationException:ModelMapper 配置错误:

1) 无效的源方法 java.util.List.add()。确保该方法具有 零参数且不返回 void。

我想我不能在PropertyMap 的配置中使用List.add()

那么,在ModelMapper 中实现List 到Set 和反向转换的最佳方式是什么?

【问题讨论】:

    标签: java type-conversion modelmapper


    【解决方案1】:

    PropertyMap 必须使用它来映射属性。如果你放一些不同的逻辑,它会抛出一个异常。

    所以,你有下一个选择(我把例子放在一个方向,另一个是一样的):

    选项 1:确保 PropertyMap 匹配属性

    使用属性映射并确保ModelMapper会使用它,看看Matching Strategies的规则:

    • 标准:默认。
    • 松散
    • 严格

    使用确保您的属性列表other 与目标属性other 匹配的选项。例如,如果您使用 Strict Strategy 并且顺序不正确,它将无法匹配。

    然后,如果您确定该属性将匹配 ModelMapper 将按照其规则为您映射该属性,或者如果您需要它,您将能够创建一个 PropertyMap 与源 MyOtherEntity 和目标 MyOtherDTO 和将其添加到您的 ModelMapper 实例中。

    public class DTOToEntityPropertyMap extends PropertyMap<MyOtherEntity, MyOtherDTO> {
      @Override
      protected void configure() {
        map().setPropertyOfOther(source.getPropertyOfOther());
        //and so on...
      }
    }
    
    modelMapper.addMappings(new DTOToEntityPropertyMap());
    

    注意:如果您的父映射(在您的情况下为 MyEntity 到 MyDto)有其他 PropertyMap,您需要在父映射之前添加列表类的 PropertyMapping,否则它不会使用这个属性映射。

    选项 2:创建列表转换器并使用它

    其他选项是创建列表类的转换器 (MyOtherEntity -> MyOtherDto) 并在父 PropertyMap 中使用它。

    Converter<Set<MyOtherEntity> , List<MyOtherDto>> toOtherDto = new Converter<Set<MyOtherEntity> , List<MyOtherDto>>() {
      public String convert(MappingContext<Set<MyOtherEntity> , List<MyOtherDto>> context) {
        Set<MyOtherEntity> source = context.getSource();
        List<MyOtherDto> destination = context.getDestination();
        //Convert it using the logic you want (by hand for example)
    
        return destination;
      }
    };
    

    那么你必须在你的 Parent PropertyMap 中使用它,如下:

    public class EntityToDTOPropertyMap extends PropertyMap<MyEntity, MyDTO> {
    
      Converter<Set<MyOtherEntity> , List<MyOtherDto>> toOtherDto = new Converter<Set<MyOtherEntity> , List<MyOtherDto>>() {
        public String convert(MappingContext<Set<MyOtherEntity> , List<MyOtherDto>> context) {
          Set<MyOtherEntity> source = context.getSource();
          List<MyOtherDto> destination = context.getDestination();
          //Convert it using the logic you want (by hand for example)
    
          return destination;
        }
      };
    
      @Override
      protected void configure() {
        using(toOtherDto).map(source.getOther()).setOther(null);
      }
    }
    

    然后确保将此 PropertyMap 添加到您的 ModelMapper 实例中:

    modelMapper.addMappings(new EntityToDTOPropertyMap());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-28
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多