有没有一种方法可以在没有getter或setter的情况下使Dozer映射字段?(或者,什么映射器可以做到这一点?)



下面是一篇关于Dozer的文章:https://www.baeldung.com/dozer.它是一个使用反射将相同名称字段从一个对象映射到另一个对象(属于完全无关的类(的映射器。

我想知道这是否适用于私有字段、getter和setter。也就是说,

  • private String a会映射到另一个对象的private String a而不具有任何getter或setter吗?

  • 如果只有一方有getter或setter(并且私有字段的名称有所不同,以确保它不会直接访问私有字段(,该怎么办?

  • 如果一个有getter,另一个有setter,用于完全不匹配的私有字段,该怎么办?(但是getter和setter名称匹配。(

我写了一个测试程序运行https://www.jdoodle.com/online-java-compiler:

import org.dozer.DozerBeanMapper;
public class Main {
public static class MySource {
// a -> a
private String a;
// getB() -> b
private String hidden_b;
public String getB() { return hidden_b; }
// c -> setC(c)
private String c;
// getD() -> setD(d)
private String hidden_d;
// proper getters and setters on both sides
private String proper;
public String getProper() { return proper; }
// public void setProper(String proper_) { proper = proper_; }
public MySource() {
a = "A Room with a View";
hidden_b = "The Bridge of San Luis Rey";
c = "Civilwarland in Bad Decline";
hidden_d = "Darkness at Noon";
proper = "This should copy, at minimum.";
}
public void print() {
System.out.println("Source");
System.out.println("================================");
System.out.println("a        = " + a);
System.out.println("hidden_b = " + hidden_b);
System.out.println("c        = " + c);
System.out.println("hidden_d = " + hidden_d);
System.out.println("--------------------------------");
System.out.println("proper   = " + proper);
System.out.println("");
}
}
public static class MyTarget {
private String a;
private String b;
private String hidden_c;
private String hidden_e;
public void setC(String param) { hidden_c = param; }
public void setD(String param) { hidden_e = param; }
private String proper;
// public String getProper() { return proper; }
public void setProper(String proper_) { proper = proper_; }
public MyTarget() {}
public void print() {
System.out.println("Target");
System.out.println("================================");
System.out.println("a        = " + a);
System.out.println("b        = " + b);
System.out.println("hidden_c = " + hidden_c);
System.out.println("hidden_e = " + hidden_e);
System.out.println("--------------------------------");
System.out.println("proper   = " + proper);
System.out.println("");
}
}
public static void main(String args[]) {
MySource s = new MySource();
s.print();
System.out.println("Now dozing...");
System.out.println("");
MyTarget t = new DozerBeanMapper().map(s, MyTarget.class);
t.print();
}
}

请注意,要运行上述代码,必须添加一个maven依赖项:

Group ID:    net.sf.dozer
Artifact ID: dozer
Version:     5.5.1

此外,您还必须尝试执行几次,因为随机超时取决于依赖项加载是否足够快。

总之,我的输出是:

Source
================================
a        = A Room with a View
hidden_b = The Bridge of San Luis Rey
c        = Civilwarland in Bad Decline
hidden_d = Darkness at Noon
--------------------------------
proper   = This should copy, at minimum.
Now dozing...
Target
================================
a        = null
b        = null
hidden_c = null
hidden_e = null
--------------------------------
proper   = This should copy, at minimum.

因此,似乎Dozer通过源上的getter和目标上的setter工作,这令人失望。或者,我没有正确使用它!

有没有办法让Dozer更灵活?或者,另一个可以实现这一点的映射程序库?

好吧,这是我的发现。希望这能帮助到别人。

Dozer 5.5.1本应能够通过"类级别是可访问的"来实现这一点。然而,出现了一个错误。它在未来的版本中得到了修复,例如Dozer 6.1+。(包移到了一个新的组org.github.dozermapper。(不过步骤有点复杂,最终我放弃了尝试ModelMapper,它要好得多。这是我的代码。

包括此包:

Group ID:    org.modelmapper
Artifact ID: modelmapper
Version:     2.3.2

以下是使用方法:

import org.modelmapper.ModelMapper;
import org.modelmapper.config.Configuration;
public class Main {
public static class MySource {
// a -> a
private String a;
// getB() -> b
private String hidden_b;
public String getB() { return hidden_b; }
// c -> setC(c)
private String c;
// getD() -> setD(d)
private String hidden_d;
// proper getters and setters on both sides
private String proper;
public String getProper() { return proper; }
// public void setProper(String proper_) { proper = proper_; }
public MySource() {
a = "A Room with a View";
hidden_b = "The Bridge of San Luis Rey";
c = "Civilwarland in Bad Decline";
hidden_d = "Darkness at Noon";
proper = "This should copy, at minimum.";
}
public void print() {
System.out.println("Source");
System.out.println("================================");
System.out.println("a        = " + a);
System.out.println("hidden_b = " + hidden_b);
System.out.println("c        = " + c);
System.out.println("hidden_d = " + hidden_d);
System.out.println("--------------------------------");
System.out.println("proper   = " + proper);
System.out.println("");
}
}
public static class MyTarget {
private String a;
private String b;
private String hidden_c;
private String hidden_e;
public void setC(String param) { hidden_c = param; }
public void setD(String param) { hidden_e = param; }
private String proper;
// public String getProper() { return proper; }
public void setProper(String proper_) { proper = proper_; }
public MyTarget() {}
public void print() {
System.out.println("Target");
System.out.println("================================");
System.out.println("a        = " + a);
System.out.println("b        = " + b);
System.out.println("hidden_c = " + hidden_c);
System.out.println("hidden_e = " + hidden_e);
System.out.println("--------------------------------");
System.out.println("proper   = " + proper);
System.out.println("");
}
}
public static void main(String args[]) {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);
MySource s = new MySource();
s.print();
System.out.println("Now dozing...");
System.out.println("");
MyTarget t = modelMapper.map(s, MyTarget.class);
t.print();
}
}

这是我的输出:

Source
================================
a        = A Room with a View
hidden_b = The Bridge of San Luis Rey
c        = Civilwarland in Bad Decline
hidden_d = Darkness at Noon
--------------------------------
proper   = This should copy, at minimum.
Now dozing...
Target
================================
a        = A Room with a View
b        = The Bridge of San Luis Rey
hidden_c = Civilwarland in Bad Decline
hidden_e = null
--------------------------------
proper   = This should copy, at minimum.

第四个案子没有复制过来,但我真的不在乎那个案子。不过,我认为使用不同的ModelMapper配置可以轻松实现。也许可以尝试松散复制。或者在最坏的情况下,在配置中手动绑定getter和setter方法。

默认情况下,Dozer使用getter和setter,但是您可以告诉Dozer(通过映射(直接访问字段http://dozer.sourceforge.net/documentation/custommethods.html

BTW、Dozer 5和6也包含基于API的映射。

相关内容

最新更新