首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ModelMapper:如何映射作为泛型传递的List<String>?

ModelMapper:如何映射作为泛型传递的List<String>?
EN

Stack Overflow用户
提问于 2020-12-22 08:09:58
回答 3查看 3.8K关注 0票数 5

我有一节抽象的课:

代码语言:javascript
运行
复制
public abstract class AbstractBeanDTO<T> extends AbstractBigDTO {
protected T value;

//getter setter 
}

扩展类具有泛型类型列表。

代码语言:javascript
运行
复制
public class ItemsBeanDTO extends AbstractBeanDTO<List<String>> {
}

我试图将模型类映射到DTO,DTO也具有相同的结构。

代码语言:javascript
运行
复制
AbstractBeanDTO<?> dto =  (AbstractBeanDTO<?>) modelMapper.map(modelBean, ItemsBeanDTO.class);

我的modelMapper配置为严格匹配。它能够转换其他泛型类型,如Long、Integer、String,但不能转换List。

我得到以下错误:

代码语言:javascript
运行
复制
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解释地映射它,但是它是一样的。

代码语言:javascript
运行
复制
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,如下所示,

代码语言:javascript
运行
复制
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)));
});

在这种情况下,我得到了以下错误:

代码语言:javascript
运行
复制
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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-23 09:55:07

最后,我找到了解决这个问题的方法。您可以使用typeMap跳过setter方法,设置列表,并使用setProvider手动设置列表。

代码语言:javascript
运行
复制
TypeMap<ItemsBean, ItemsBeanDTO> typeMap = modelMapper.createTypeMap(ItemsBean.class,
                ItemsBeanDTO.class);

        typeMap.addMappings(mapper -> mapper.skip(ItemsBeanDTO::set));

        typeMap.setProvider(request -> {
            ItemsBean source = ItemsBean.class.cast(request.getSource());
            ItemsBeanDTO destination = new ItemsBeanDTO();
            destination.set(source.get());
            return destination;
        });
票数 1
EN

Stack Overflow用户

发布于 2020-12-22 08:26:43

List是一个接口,没有构造函数。ModelMapper需要一个混凝土级,因此您需要选择List的实现,包括ArrayListLinkedList等。

票数 2
EN

Stack Overflow用户

发布于 2021-01-05 03:58:51

您可以使用我的库豆刀代替。它是一个注释处理器。这意味着它动态地生成DTO类。您可以检查生成的类的来源,不再有魔力。转换器方法嵌入到生成的类中。它们并不复杂,但它们很麻烦,因此自动化是必要的。您可以筛选从原始类继承的属性,甚至可以更改或向生成的类添加新属性。库提供了一种通过注释实现属性转换的方法。例如,如果Integer为null,则将其转换为零。您还可以编写自己的实现。

例如:

代码语言:javascript
运行
复制
class Pojo1 {
    String a;
    Pojo b;
}

class Pojo2 {
    Pojo1 a;
    List<Pojo1> b;
    Map<List<Pojo1>>[] c;
}

// target Pojo1, and all properties will be included except 'b'
@ViewOf(value = Pojo1.class, includePattern = ".*", excludes={Pojo1Meta.b})
class ConfigureOfPojo2 {}

// target Pojo2, and all properties will be included
@ViewOf(value = Pojo2.class, includePattern = ".*")
class ConfigureOfPojo2 {
    // convert b to dto version. 
    // Pojo1View is the generated dto class of Pojo1.
    // Of course, you can change it. But here use the default name for simpify.
    // If you want change the type or value of a existing property,
    // You should use @OverrideViewProperty.
    // Convert to dto version is internal supported.
    // For more complex case, you may need to use a method to define how the property should be deal with.
    @OverrideViewProperty(Pojo2Meta.b)
    private List<Pojo1View> b;

    // Convert c to dto version
    // BeanKnife support convert list, set, map, array of object to its dto version, only if they has the same shape.
    @OverrideViewProperty(Pojo2Meta.c)
    private Map<List<Pojo1View>>[] c;
}

会产生

代码语言:javascript
运行
复制
// meta class, you can use it to reference the property name in a safe way.
class Pojo1Meta {
    public final String a = "a";
    public final String b = "b";
}

// generated DTO class. The actual one will be more complicate, there are many other method.
class Pojo1View {
    private String a;
    public Pojo1View read(Pojo1 source) { ... }
    ... getters and setters ...
}

class Pojo2Meta {
    public final String a = "a";
    public final String b = "b";
    public final String c = "c";
}

class Pojo2View {
    private String a;
    private List<Pojo1View> b;
    private Map<List<Pojo1View>>[] c;
    public Pojo2View read(Pojo2 source) { ... }
    ... getters and setters ...
}

下面是更多的示例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65405156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档