有没有一种方法可以使Dozer映射字段不使用getter或setter方法? (或者,哪个映射器可以做到这一点?)

时间:2018-12-04 15:00:53

标签: java dozer

这是有关推土机的文章:https://www.baeldung.com/dozer。这是一个映射器,它使用反射将同名字段从一个对象映射到另一个对象(完全不相关的类)。

我想知道这是否可以灵活地用于私有字段,getter和setter。也就是说,

  • private String a会映射到另一个对象的private String a而没有任何获取器或设置器吗?

  • 如果只有一侧具有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进行工作,这令人失望。或者,我没有正确使用它!

有没有办法使推土机更加灵活?或者,另一个可以实现此目标的映射器库?

2 个答案:

答案 0 :(得分:0)

好的,这是我的发现。希望这对某人有帮助。

推土机5.5.1应该能够通过“可访问类级”来做到这一点。但是,有a bug。它已针对将来的版本例如 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配置轻松实现。也许尝试LOOSE复制。或更糟糕的是,在配置中手动绑定getter和setter方法。

答案 1 :(得分:0)

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

BTW,推土机5和6也包含基于API的映射。