简体   繁体   English

推土机从具有地图的对象映射到具有属性的Pojo

[英]Dozer Mapping from Object with Map to Pojo with properties

I am trying to do a oneWay Mapping with Dozer from Source to Destination. 我正在尝试使用Dozer从源到目的地进行一次映射。

public class Source {
    Map<String, String> values;
    public Source() {
    }
    public Source(Map<String, String> values) {
        this.values = values;
    }
    public Map<String, String> getValues() {
        return values;
    }
    public void setValues(Map<String, String> values) {
        this.values = values;
    }
}

.

public class Destination {
    private String lastname;
    private String firstname;
    public Destination() {
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
}

Here is my Testclass: 这是我的Testclass:

public class DozerMapperTest {
    private DozerMapper mapper = new DozerMapper();
    @Test
    public void testName() throws Exception {
        String firstname = "Tom";
        String lastname = "Hanks";
        Map<String, String> input = new HashMap<>();
        input.put("firstname", firstname);
        input.put("lastname", lastname);
        Destination result = mapper.map(new Source(input));
        Assert.assertNotNull(result);
        Assert.assertEquals(firstname, result.getFirstname());
        Assert.assertEquals(lastname, result.getLastname());
    }
}

My Mapping Class looks like this: 我的Mapping类看起来像这样:

public class DozerMapper {
    public DozerMapper() {
        initMapper();
    }
    private DozerBeanMapper mapper;
    public Destination map(final Source input) {
        return mapper.map(input, Destination.class);
    }
    void initMapper() {
        BeanMappingBuilder builder = new BeanMappingBuilder() {
            @Override
            protected void configure() {
                mapping(Source.class, Destination.class, TypeMappingOptions.oneWay())
                        .fields(new FieldDefinition("values.lastname"), "lastname")
                        .fields(new FieldDefinition("values.firstname"), "firstname");
            }
        };
        mapper = new DozerBeanMapper();
        mapper.addMapping(builder);
    }
}

But his is all not working :-( I also tried this mapping: 但他的一切都不起作用:-(我也试过这个映射:

.fields(new FieldDefinition("values").mapKey("lastname"), "lastname")
.fields(new FieldDefinition("values").mapKey("firstname"), "firstname");

I googled, looked in the dokumentation and nothing. 我用谷歌搜索,看着dokumentation,没有。 Can anybody help me or give me some hints? 任何人都可以帮助我或给我一些提示吗?

I myself didn't think this was possible until I researched it just now, so thanks for the question! 在我刚才研究之前,我自己并不认为这是可能的,所以谢谢你的问题!

The correct way to do is this: 正确的方法是:

.fields("values", "firstname")
.fields("values", "lastname");

The key point here is that the variables "firstname" and "lastname" in Destination class has to be the same as the keys in the Source class' Map. 这里的关键点是Destination类中的变量“firstname”和“lastname”必须与Source类的Map中的键相同。 Otherwise this won't work. 否则这将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM