[英]Dozer Mapper is not mapping directly
I have a SourceClass with following parameters as: 我有一个带有以下参数的SourceClass:
class SourceClass{
public Integer a;
public Integer b;
}
And a DestinationClass as: 而DestinationClass为:
class DestinationClass {
public Integer a;
public Integer b;
}
And here is my test code: 这是我的测试代码:
public static void main(String[] args) {
Mapper mapper = new DozerBeanMapper();
SourceClass src= new SourceClass();
src.a= 1;
src.b= 2;
DestinationClass dest = mapper.map(src, DestinationClass.class);
System.out.println(dest.a + " " + dest.b);
}
The last line of the code is showing as null null , now I have tried by giving the getter/setter as well but didn't worked, I finally got the output by specifying @Mapping annotation giving the name of the variable to map like @Mappinf("a"), but as you see my variable names are same, can't dozermapper do it by itself?Because here it is written that it maps the same named variables automatically. 代码的最后一行显示为null null ,现在我也尝试通过给定getter / setter方法 ,但没有成功,我最终通过指定@Mapping注释获得了输出,该变量给出了要映射的变量名称,例如@ Mappinf(“ a”),但是您看到的变量名相同,所以dozermapper不能单独执行它吗?因为在这里写成它会自动映射相同的命名变量。
Ok so first of all either change SourceClass variables to Strings or change src.a and src.b values to be Integers. 好的,首先将SourceClass变量更改为String或将src.a和src.b值更改为Integers。
Secondly you need to have getters and setters in both SourceClass and DestinationClass because dozer relies on them regardless if the variables are public or private. 其次,您需要在SourceClass和DestinationClass中都具有getter和setter,因为无论变量是公共变量还是私有变量,推土机都依赖它们。
The following solution works: 以下解决方案有效:
public class SourceClass{
private Integer a;
private Integer b;
public Integer getA(){
return a;
}
public void setA(Integer a){
this.a = a;
}
public Integer getB()
{
return b;
}
public void setB(Integer b){
this.b = b;
}
}
public class DestClass{
private Integer a;
private Integer b;
public Integer getA(){
return a;
}
public void setA(Integer a){
this.a = a;
}
public Integer getB(){
return b;
}
public void setB(Integer b){
this.b = b;
}
}
public static void main(String[] args)
{
Mapper mapper = new DozerBeanMapper();
SourceClass src = new SourceClass();
src.setA(1);
src.setB(2);
DestClass dest = mapper.map(src, DestClass.class);
System.out.println(dest.getA() + " " + dest.getB());
}
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.