ModelMapper - 无法将 ArrayList 转换为 List
Posted
技术标签:
【中文标题】ModelMapper - 无法将 ArrayList 转换为 List【英文标题】:ModelMapper - Failed to convert ArrayList to List 【发布时间】:2017-06-03 13:40:36 【问题描述】:我有以下 Beans
public class Ticket extends BaseEntity
private ForeignCollection< TicketDetail > ticketDetails;
public class TicketDto extends BaseDto
private List< TicketDetailDto > ticketDetails;
我正在使用 ModelMapper 将 List<TicketDto>
映射到 List<Ticket>
之类的
List<TEntity> entities = mapper.map( dtoList, new TypeToken<List<Ticket>>().getType() );
但它会抛出以下异常和堆栈跟踪
1) Converter org.modelmapper.internal.converter.CollectionConverter@5d884430 failed to convert java.util.ArrayList to java.util.ArrayList.
1 error
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:374)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:69)
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:497)
at org.modelmapper.ModelMapper.map(ModelMapper.java:429)
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:
1) Failed to set value '' on com.pichardo.metalesbc.infrastructure.domainentities.Ticket.setTicketDetails()
1 error
at org.modelmapper.internal.Errors.toMappingException(Errors.java:258)
at org.modelmapper.internal.PropertyInfoImpl$MethodMutator.setValue(PropertyInfoImpl.java:118)
at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:250)
at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:181)
at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:132)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:101)
at org.modelmapper.internal.converter.IterableConverter.convert(IterableConverter.java:49)
at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:337)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:96)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:60)
... 50 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.modelmapper.internal.PropertyInfoImpl$MethodMutator.setValue(PropertyInfoImpl.java:116)
它似乎从 Ticket -> TicketDto 映射它,但它在 TicketDto -> Ticket 时失败。
非常感谢任何帮助。
【问题讨论】:
【参考方案1】:有点愚蠢,但它 ORMLite 也接受 Collection
用于外国收集字段。所以:
public class Ticket extends BaseEntity
private Collection< TicketDetail > ticketDetails;
【讨论】:
以上是关于ModelMapper - 无法将 ArrayList 转换为 List的主要内容,如果未能解决你的问题,请参考以下文章
ModelMapper 中的 ClassCastException:无法强制转换 EnhancerByModelMapper
无法在 ModelMapper 中实例化目标 [AbstractClass] 错误的实例