我有以下课程:
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。
我想我不能List.add()
在PropertyMap
.
那么,实现 List 到 Set 和向后转换的最佳方法是ModelMapper
什么?