简体   繁体   English

在dozer中使用中间集合映射深层属性

[英]Mapping deep properties with intermediate collections in dozer

Suppose I have the following classes 假设我有以下课程

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>();
}

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? 有没有我可以使用的映射,给定一个Baz,将它映射到目标类并获得包含在Baz中的foo中的字符串列表? 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>

Because string is not a property of foos (which is of type List). 因为string不是foos的属性(属于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. 如果它遇到深度映射中的集合,并且目标也是一个集合,我可以认为Dozer会足够聪明,能够将深层属性名称分解为两个并迭代整个集合以获取子元素集合成员的深度映射。 Apparently not. 显然不是。 Is there a solution short of making a feature request of Dozer? 有没有提出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>

And implement CustomFooConverter to get string field of foo and return it as a String. 并实现CustomFooConverter以获取foo的字符串字段并将其作为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>

into Dozer GitHub 进入Dozer GitHub

You could always write your own CustomConverter . 您可以随时编写自己的CustomConverter

It makes sense why Dozer isn't able to handle this as you expect since at runtime it has no type information about the List foos and can't guarantee that every Object in the list is actually a Foo . 因为Dozer无法像你期望的那样处理它,这是有道理的,因为在运行时它没有关于List foos类型信息,并且不能保证列表中的每个Object实际上都是Foo

I think you can do it without custom converter. 我想你可以不用自定义转换器就可以做到。

Override the toString() method of Foo class like below: 覆盖Foo类的toString()方法,如下所示:

@Override
public String toString(){
return this.getString(); //assuming string property has a getter method. if not,write this.string

And now the follwing mapping: 现在,以下是映射:

<mapping>
<class-a>fully qualified name of Baz(with package name)</class-a>
<class-b>same for Target</class-b>
<field>
   <a>foos</a>
   <b>fooStrings</b>
   <a-hint>foo</a-hint>
   <b-hint>java.lang.String</b-hint>
</field>
</mapping>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM