【发布时间】: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