将深层属性与推土机中的中间集合映射
假设我有以下类
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>();
}
,给定一个 Baz,我可以使用任何映射将其映射到目标类并获取 Baz 中 foo 中包含的字符串列表吗?以下映射不起作用,
<mapping>
<class-a>Baz</class-a>
<class-b>Target</class-b>
<field>
<a>foos.string</a>
<b>fooStrings</b>
</field>
</mapping>
因为 string 不是 foos 的属性(其类型为 List)。我本以为 Dozer 会足够聪明,如果它在深度映射中遇到一个集合,并且目标也是一个集合,能够将深度属性名称分成两部分并迭代集合以获取子部分来自集合成员的深度映射。显然不是。除了提出 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? 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). 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. Apparently not. Is there a solution short of making a feature request of Dozer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您始终可以编写自己的 CustomConverter。
Dozer 无法按照您的预期处理此问题是有道理的,因为在运行时它没有有关
List foos
的类型信息,并且不能保证中的每个Object
该列表实际上是一个Foo
。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 everyObject
in the list is actually aFoo
.我认为,您可以编写这样的映射
并实现 CustomFooConverter 来获取 foo 的字符串字段并将其作为字符串返回。
我认为您可以发布一个功能请求来支持映射到基元,就像
映射到 Dozer GitHub
I think, you can write such a mapping
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
into Dozer GitHub
我认为你可以在没有自定义转换器的情况下做到这一点。
重写 Foo 类的 toString() 方法,如下所示:
现在是以下映射:
I think you can do it without custom converter.
Override the toString() method of Foo class like below:
And now the follwing mapping: