繁体   English   中英

如何使用 Dozer 将数组映射到复杂类型

[英]How to mapp an array to a complex type using Dozer

我正在使用 Dozer 到 map 一些豆子,我有一个我无法弄清楚的映射。

这是我的课程:

class A{
    private ComplexeType type;
    //Constructors & getters    
}

class B{
    private String[] type;
    //Constructors & getters 
}

class ComplexteType{
     private List<String> list;
    //Getter for the list, no constructor
}

我如何将 map class A 转为 class B?

我想使用 xml 将 map 的字段类型 class A 转换为 class B 的字段类型。

这是 xml 文件:

<mapping>
        <class-a>A</class-a>
        <class-b>B</class-b>
        <field custom-converter="AToBCustomConverter">
            <a>type</a>
            <b>type</b>
        </field>
    </mapping>

这是我的 CustomConverter 的一个片段

if (source == null) {
            return null;
        }
        B dest = null;
        if (source instanceof java.lang.String) {
            // check to see if the object already exists
            if (destination == null) {
                dest = new A();
            } else {
                dest = (A) destination;
            }
            dest.getTypes().add((String) source);
            return dest;
        } else if (source instanceof B) {
            String[] sourceObj = ((B) destination)
                    .getType()
                    .toArray(
                            new String[((B) destination)
                                    .getType().size()]);
            return sourceObj;
        } else {
            throw new MappingException(
                    "Converter StatResultCustomConverter used incorrectly. Arguments passed in were:"
                            + destination + " and " + source);
        }
    }

我认为在这种情况下您的CustomConverter不是必需的,请参见此处

在您的映射文件中试试这个:

<mapping>
  <class-a>A</class-a>
  <class-b>B</class-b>
  <field>
    <a>type.list</a>
    <b>type</b>
  </field>
</mapping>

Dozer应该自动执行嵌套映射。

这是我用来解决问题的映射。

<mapping>
  <class-a>Q</class-a>
  <class-b>B</class-b>   
  <field>
    <a is-accessible="true">type<list</a>
    <b is-accessible="true">type</b>
  </field>
</mapping>

暂无
暂无

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

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