简体   繁体   English

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

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

I am using Dozer to map some beans, and I have a mapping that I can't figure out.我正在使用 Dozer 到 map 一些豆子,我有一个我无法弄清楚的映射。

Here are my classes:这是我的课程:

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
}

How do I map class A to class B?我如何将 map class A 转为 class B?

I want to map the field type of class A to the field type of the class B using xml.我想使用 xml 将 map 的字段类型 class A 转换为 class B 的字段类型。

here is the xml file:这是 xml 文件:

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

And here is a sippet from my CustomConverter这是我的 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);
        }
    }

I don't think your CustomConverter is necessary in this case, see here .我认为在这种情况下您的CustomConverter不是必需的,请参见此处

Try this in your mapping file:在您的映射文件中试试这个:

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

and Dozer should perform the nested mapping automatically. Dozer应该自动执行嵌套映射。

Here is the mapping I used to solve the problem.这是我用来解决问题的映射。

<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