在dozer中使用中间集合映射深层属性 [英] Mapping deep properties with intermediate collections in dozer

查看:312
本文介绍了在dozer中使用中间集合映射深层属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下类

public class Baz {
  private List<Foo> foos = new ArrayList<Foo>();
}

public class Foo {
  private String string;
}

public class Target {
  private List<String> fooStrings = new ArrayList<String>();
}

我是否可以使用任何映射,给定Baz,将其映射到目标类并获取Baz中foo中包含的字符串列表?以下映射不起作用

Is there any mapping I can use to, given a Baz, map it to the target class and get a list of the strings contained within the foo's in Baz? The following mapping does not work

<mapping>
  <class-a>Baz</class-a>
  <class-b>Target</class-b>
  <field>
    <a>foos.string</a>
    <b>fooStrings</b>
  </field>
</mapping>

因为字符串不是foos的属性(属于List类型)。我原以为Dozer会非常聪明,如果遇到深度映射中的集合,并且目标也是一个集合,能够将深层属性名称分解为两个并迭代整个集合以获取子元素集合成员的深度映射。显然不是。有没有提出Dozer功能请求的解决方案?

Because string is not a property of foos (which is of type List). I would have thought Dozer would be clever enough to, if it encountered a collection in a deep mapping, and the target was also a collection, to be able to break the deep property name into two and iterate across the collection to get the child part of the deep mapping from the collection members. Apparently not. Is there a solution short of making a feature request of Dozer?

推荐答案

我想,你可以编写这样的映射

I think, you can write such a mapping

<mapping>
  <class-a>Baz</class-a>
  <class-b>Target</class-b>
  <field>
    <a>foos</a>
    <b>fooStrings</b>
  </field>
</mapping>

<custom-converters> 
  <converter type="CustomFooConverter">
    <class-a>
      Foo
    </class-a>
    <class-b>
      String
    </class-b>
  </converter>
</custom-converters>

并实现CustomFooConverter以获取foo的字符串字段并将其作为字符串返回。

And implement CustomFooConverter to get string field of foo and return it as a String.

我认为您可以发布功能请求以支持映射到原语

I think you can post a feature request to support mapping to primitives as

<mapping>
  <class-a>Foo</class-a>
  <class-b>String</class-b>
  <field>
    <a>string</a>
  </field>
</mapping>

进入Dozer GitHub

这篇关于在dozer中使用中间集合映射深层属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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