gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:52:50 26 4
gpt4 key购买 nike

我正在使用 Dozer 来映射一些 bean,但我有一个我无法弄清楚的映射。

这是我的类(class):

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
}

如何将 A 类映射到 B 类?

我想使用xml将A类的字段类型映射到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 是不必要的,请参阅 here .

在您的映射文件中尝试此操作:

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

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

关于java - 如何使用 Dozer 将数组映射到复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com