地图使用推土机的定制转换对象到另一个目录列表 [英] Map a list of object to another list using Dozer's custom converters

查看:193
本文介绍了地图使用推土机的定制转换对象到另一个目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是实体列表映射到其字符串ID列表使用推土机(或多或少)。

What I am trying to do is to map a List of entities to a list of their String ids (more or less) using Dozer.

显然,这意味着自定义转换器。我的第一个想法是使从myEntity所转换器为一个字符串,然后说推土机类似地图使用该转换器的这个集合的每个对象。但我无法弄清楚如何做到这一点。

Obviously, it implies Custom Converter. My first idea was to make a converter from MyEntity to a String, and then say to Dozer something like "Map every object of this collection using this converter". But I couldn't figure out how to do so.

所以我的第二个想法是让转换器形成实体的列表字符串列表,直接。我对这个想法的问题是,我是strugling的东西可笑的是让我的列表类型在构造函数中,如下(不运行):

So my second idea was to make a converter form a list of entities to a list of string, directly. My problem on this idea is that I was strugling on something ridiculous which is to get the type of my list in the constructor, as below (which doesn't work at all):

public MyEntityListConverter() {
    super(List<MyEntity>.class, List<String>.class);
}

我不知道如何在一个单行传递一个实例列表的类wihout宣布任何事情。

I don't know how to pass an instantiated list's class in a single row wihout declaring anything.

因此​​,如果有人知道或者:

So if someone know either :


  • 如何指定推土机对象转换器在集合映射使用

  • 如何获得实例化列表类型

  • 第三/更好的解决方案来尝试

推荐答案

在尝试的方式是不可能的,因为泛型类型。而如果是,推土机无法在运行时检测类型​​。

The way you tried is not possible due to generic types. And if it was, Dozer cannot detect types at runtime.

1的解决方案列表与LT;&GT;

您转换器:

public class MyEntityToStringConverter extends DozerConverter<MyEntity, String> {
    // TODO constructor + impl
}

您映射:

mapping(MyEntityA.class, MyEntityB.class)
.fields("myEntityList", "myStringList",
    hintA(MyEntity.class),
    hintB(String.class));

mapping(MyEntity.class, String.class)
.fields(this_(), this_(), customConverter(MyEntityToStringConverter.class));

第2解决方案与清单包装

您可以尝试创建一个扩展列表implement执行你的自定义类。

You can try to create your custom classes extending a list impl.

public class MyEntityList extends ArrayList<MyEntity> {

}

public class MyStringList extends ArrayList<String> {

}

更改您要映射的父类的字段。

Change your field in the parent classes you want to map.

您转换器:

public class MyEntityToStringConverter extends DozerConverter<MyEntityList, MyStringList> {
    // TODO constructor + impl
}

您映射:

mapping(MyEntityA.class, MyEntityB.class)
.fields("myEntityList", "myStringList", customConverter(MyEntityToStringConverter.class));

这篇关于地图使用推土机的定制转换对象到另一个目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆