私は次のクラスを持っています:
public class MyEntity {
private Set<MyOtherEntity> other;
}
public class MyDTO {
private List<MyOtherDTO> other;
}
DTO との間の変換ごとに 1 つずつ、PropertyMaps
( を使用して) 2 つ作成しましたModelMapper
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
ですか?