Mapping deep properties with intermediate collections in dozer

心不动则不痛 提交于 2019-12-03 07:37:07

You could always write your own 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.

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.

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

I think you can do it without custom converter.

Override the toString() method of Foo class like below:

@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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!