如何在 Dozer 映射期间定义中间类的创建方法
这是我的场景,我有 2 个类“com.project.ClassA”和“com.project.ClassB”,我试图将字段 ClassA.name 映射到 ClassB.person.nameObj.firstName。
正如你所看到的,Dozer 在进行映射之前需要创建 person 对象和 nameObj,有一些工厂类来初始化 Person 和 NameObj 对象。
我知道我们可以在字段和类级别定义自定义创建方法,但这仅适用于您正在映射的字段或类。在我的示例中,映射是在 2 个字符串字段之间进行的,但在执行映射之前需要创建 2 个对象。
我现在遇到的错误是 org.dozer.MappingException: java.lang.NoSuchMethodException: com.project.ClassB.Person.()
任何帮助或想法将不胜感激。谢谢。
Here's my scenario, I have 2 classes "com.project.ClassA" and "com.project.ClassB", I'm trying to map the fields ClassA.name to ClassB.person.nameObj.firstName.
As you can see, Dozer needs to create person object and nameObj before it can do the mapping, there is some factory classes to initiate the Person and NameObj objects.
I know we can define custom create method in field and class level, but that only applies to the field or the class that you are mapping. In my example, the mapping are between 2 String fields, but 2 objects are required to be created before the mapping is performed.
The error I'm getting now is
org.dozer.MappingException: java.lang.NoSuchMethodException: com.project.ClassB.Person.()
Any helps or ideas will greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两件事
1. ClassB需要一个构造函数来初始化obj Person,同样Person也需要一个构造函数来初始化nameobj。
2. 应该存在访问器和修改器方法(getter 和 setter)。
完成这两个步骤后,这应该可以工作。
Two things
1. ClassB needs a constructor which initialized obj Person, similarly Person needs a constructor to initialize nameobj.
2. accessor and mutator methods should be present (getters and setters).
After you do both these steps this should work.