【问题标题】:Dozer Mapper does not map despite fields having same names and XML mappings specified尽管指定了相同名称和 XML 映射的字段,Dozer Mapper 仍不映射
【发布时间】:2020-02-05 12:08:02
【问题描述】:

假设我有:

  1. ObjectWithListListWrapperObjectWithWrapper 类,如下所示:
//com.github.komidawi.ObjectWithList
public class ObjectWithList {
    private List<String> list;
    // ...
}

//com.github.komidawi.ListWrapper 
public class ListWrapper {
    private List<String> wrappedList;
    // ...
}

//com.github.komidawi.ObjectWithWrapper
public class ObjectWithWrapper {
    private ListWrapper list;
    // ...
}
  1. MyCustomConverter 类来包装/解包这些列表。

  2. ma​​pperConfiguration.xml配置文件

<!-- ... -->
<configuration>
    <stop-on-errors>true</stop-on-errors>
    <wildcard>true</wildcard>

    <custom-converters>
        <converter type="com.github.komidawi.MyCustomConverter">
            <class-a>com.github.komidawi.ListWrapper</class-a>
            <class-b>java.util.List</class-b>
        </converter>
    </custom-converters>
</converter>

<mapping>
        <class-a>com.github.komidawi.ObjectWithList</class-a>
        <class-b>com.github.komidawi.ObjectWithWrapper</class-b>
</mapping>

我想将 ObjectWithWrapper 映射到 ObjectWithList

// ... creating objects
ObjectWithList objectWithList = mapper.map(objectWithWrapper, ObjectWithList.class);
objectWithList.getList(); // This is null

很遗憾,映射后objectWithList的list字段为null。 我使用了调试器,在这种情况下,Dozer 甚至没有输入 MyCustomConverter

令人惊讶的是,不同方向的映射工作正常:

ObjectWithWrapper objecthWithWrapper = mapper.map(objectWithList, ObjectWithWrapper.class);
objecthWithWrapper.getList(); // This is not null

总结:

  1. ObjectWithListObjectWithWrapper 中的字段名称相同:list
  2. 我已经在 xml 文件中为 Java 的 ListListWrapper 指定了自定义转换器
  3. 我已经在 xml 文件中指定了 ObjectWithListObjectWithWrapper 之间的映射
  4. 在这两种情况下,Dozer 都会说:“从 URL 成功加载自定义 xml 映射”
  5. 当从 ObjectWithList 映射到 ObjectWithWrapper 时,它工作正常

而问题是:

映射后,objectWithListnull 而不是实际的列表。我发现它甚至没有进入 MyCustomConverter

【问题讨论】:

    标签: java mapping converters dozer mapper


    【解决方案1】:

    集合很特别,类型擦除使它们更具挑战性。如果您只想将 WrappedList 中的项目放在生成的 ObjectWithList 实例的列表中,则无需转换器即可完成。 Dozer 将处理嵌套属性映射,因此您可以通过指示 Dozer 将列表相互映射的方式来描述映射。

    new BeanMappingBuilder() {
        configure() {
            mapping(ObjectWithList.class, ObjectWithWrapper.class,
                .fields("list.wrappedList", "list")
        }
    }
    

    或在 XML 中

    <mapping>
            <class-a>com.github.komidawi.ObjectWithList</class-a>
            <class-b>com.github.komidawi.ObjectWithWrapper</class-b>
            <field>
                <a>list.wrappedList</a>
                <b>list</b>
            </field>
    </mapping>
    

    【讨论】:

    • 假设wrappedListList&lt;A&gt;类型,listList&lt;B&gt;类型,不使用自定义转换器是否可以实现ObjectWithListObjectWithWrapperA的映射到B 列表元素映射以及?
    • @komidawi 是的。然后,Dozer 将遍历列表并将源中的每个元素 A 映射到目标中的元素 B 实例。
    • @komidawi 这解决了您的问题吗?如果您能接受答案,我将不胜感激。谢谢约翰
    • 确实可以避免使用自定义转换器——我只需要明确指定 XML 文件中的所有字段。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2019-12-17
    • 2016-04-04
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多