gpt4 book ai didi

java - ModelMapper:如何映射作为泛型传递的 List

转载 作者:行者123 更新时间:2023-12-04 03:44:23 33 4
gpt4 key购买 nike

我有一个抽象类:

public abstract class AbstractBeanDTO<T> extends AbstractBigDTO {
protected T value;

//getter setter
}
扩展类具有泛型类型 List
public class ItemsBeanDTO extends AbstractBeanDTO<List<String>> {
}
我试图将模型类映射到也具有相同结构的 DTO。
AbstractBeanDTO<?> dto =  (AbstractBeanDTO<?>) modelMapper.map(modelBean, ItemsBeanDTO.class);
我的 modelMapper 配置为严格匹配。它能够转换其他泛型类型,如 Long、Integer、String 但不能转换 List。
我收到以下错误:
org.modelmapper.MappingException: ModelMapper mapping errors:

1) Failed to instantiate instance of destination java.util.List. Ensure that java.util.List has a non-private no-argument constructor.
Caused by: java.lang.NoSuchMethodException: java.util.List.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.modelmapper.internal.MappingEngineImpl.instantiate(MappingEngineImpl.java:333)
at org.modelmapper.internal.MappingEngineImpl.createDestination(MappingEngineImpl.java:348)
at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:141)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:115)
at org.modelmapper.internal.converter.MergingCollectionConverter.convert(MergingCollectionConverter.java:59)
at org.modelmapper.internal.converter.MergingCollectionConverter.convert(MergingCollectionConverter.java:31)
at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:303)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:110)
at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:242)
at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:188)
at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:152)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:115)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:72)
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:573)
at org.modelmapper.ModelMapper.map(ModelMapper.java:406)
我尝试使用 TypeMap 显式地映射它,但它是一样的。
modelMapper.createTypeMap(ItemsBean.class, ItemsBeanDTO.class).addMappings(mapper -> {
mapper.map(src -> src.getValue(), (dest, value) -> dest.setValue((List<String>) value));
});
唯一有效的是当您将变量显式声明为 List 时。 (但我不想这样做)
我正在使用 2.3.5 版本的 ModelMapper 和 Java8。
谁能帮助我这将如何工作?

我尝试使用 typeMap 和 typeToken 如下,
Type listType = new TypeToken<ItemsBeanDTO>() {
}.getType();

modelMapper.createTypeMap(ItemsBean.class, ItemsBeanDTO.class).addMappings(mapper -> {
mapper.map(src -> src.getValue(), (dest, value) -> dest.setValue(modelMapper.map(value, listType)));
});
在这种情况下,我得到以下错误:
java.lang.IllegalArgumentException: source cannot be null
at org.modelmapper.internal.util.Assert.notNull(Assert.java:53)
at org.modelmapper.ModelMapper.map(ModelMapper.java:493)
at com.lowteq.model.preop.controller.config.BeanModelMapperFactory.lambda$22(BeanModelMapperFactory.java:117)
at org.modelmapper.internal.ReferenceMapExpressionImpl.map(ReferenceMapExpressionImpl.java:68)
at org.modelmapper.internal.ConfigurableConditionExpressionImpl.map(ConfigurableConditionExpressionImpl.java:65)
at com.lowteq.model.preop.controller.config.BeanModelMapperFactory.lambda$20(BeanModelMapperFactory.java:117)
at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:266)

最佳答案

List 是一个接口(interface),没有构造函数。 ModelMapper需要 concrete class ,因此您需要更喜欢 List 的实现包括ArrayListLinkedList等等

关于java - ModelMapper:如何映射作为泛型传递的 List<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65405156/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com